poj 3616(动态规划)

Milking Time

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7265   Accepted: 3043

Description

Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible.

Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houriN), an ending hour (starting_houri < ending_houriN), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.

Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ RN) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.

Input

* Line 1: Three space-separated integers: N, M, and R
* Lines 2..M+1: Line i+1 describes FJ‘s ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi

Output

* Line 1: The maximum number of gallons of milk that Bessie can product in the N hours

Sample Input

12 4 2
1 2 8
10 12 19
3 6 24
7 10 31

Sample Output

43

Source

USACO 2007 November Silver

题意:挤奶工工作的区间是 1 - N (hour),农场主有m个时间段要挤奶工挤奶,每一个时间段都有一个 起始时间 结束时间 以及在这段时间内可以获得的利益 。而且挤奶工每工作

完一个时间段就要休息r分钟,求挤奶工在 这m个时间段中能够获得的最大利益。

题解:dp[i]表示在前 i 段时间挤奶工能够获得的最大收益,对前m段时间按起始时间升序排个序,起始时间相同按照结束时间升序排列。我们就可以得到状态转移方程. dp[i] = max(dp[j]+value[i],dp[i])&&mk[j].e+r<=mk[i].s (1=<j<i)

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 1000005;
const int M = 1005;
int dp[N]; ///dp[i]表示在前i组时间中能取得的最大利益
struct Milk{
    int s,e,v;
}mk[M];
int cmp(Milk a,Milk b){
    if(a.s!=b.s) return a.s<b.s;
    return a.e<b.e;
}
int main()
{
    int n,m,r;
    while(scanf("%d%d%d",&n,&m,&r)!=EOF)
    {
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&mk[i].s,&mk[i].e,&mk[i].v);
        }
        memset(dp,0,sizeof(dp));
        sort(mk+1,mk+1+m,cmp);
        int mx = -1;
        for(int i=1;i<=m;i++){
            dp[i] = mk[i].v;
            for(int j=1;j<i;j++){
                if(mk[j].e+r<=mk[i].s&&mk[i].v+dp[j]>dp[i]){
                    dp[i] = dp[j] +mk[i].v;
                }
            }
            if(dp[i]>mx) mx = dp[i];
        }
        printf("%d\n",mx);
    }
    return 0;
}
时间: 2024-11-07 20:46:57

poj 3616(动态规划)的相关文章

[2016-03-28][POJ][3616][Milking Time]

时间:2016-03-28 17:27:03 星期一 题目编号:[2016-03-28][POJ][3616][Milking Time] #include <algorithm> #include <cstdio> using namespace std; const int maxm = 1000 + 10; struct Roo{ int l,r,v; bool operator < (const Roo & a)const{ return l < a.l

POJ 3616 Milking Time DP题解

典型的给出区间任务和效益值,然后求最大效益值的任务取法. 属于一维DP了. 一维table记录的数据含义:到当前任务的截止时间前的最大效益值是多少. 注意, 这表示当前任务一定要选择,但是最终结果是不一定选择最后一个任务,故此最后需要遍历找到table数组的最大值,当然计算过程中使用一个数记录最终最大值也是可以的. 状态转移方程就是: tbl[i] = MAX({from tbl[0]->tbl[i-1] }+ weight[i] ),即区间0到i-1加上i的当前效益值. #include <

poj 1458 动态规划DP

//  poj 1458  zoj 1733  最长公共子序列  DP #include <iostream>#include <string.h>#define N 1005using namespace std ;char  s1[N],s2[N];   int dp[N][N];int max(int a,int b)   {    return a>b ? a : b ;  }void f(int n,int m){   int i,j;    for (i=0; i

R - Milking Time POJ 3616 ( DP )

R - Milking Time Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3616 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedul

poj 3616 Milking Time dp+树状数组

题意: 给一堆区间,每个区间有开始时间s,结束时间e,和收益w,现在要找一些区间使收益和最大,且区间之间的间隔最小为r. 分析: 这道题用dp做是简单题,用dp+树状数组做是中等题.dp问题的关键是对状态的定义.有两种方法,一:dp[k]表示按开始时间排序到第k个区间能取得的最大收益.二:dp[t]表示在时间t时能获得的最大收益.定义好状态方程就好写了这不再赘述.有趣的是这个时间复杂度.设一共有M个区间,所有区间的最大时间为L,第一种是M^2的,第二种是M*(logL+logM)的,这题M才10

POJ 3616 Milking Time 挤奶问题,带权区间DP

题目链接:POJ 3616 Milking Time Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4837   Accepted: 2034 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to sc

POJ 3616 Milking Time 动态规划

http://poj.org/problem?id=3616 题意:一个奶牛在0~N时间段内可被取奶,每次挤奶以后必须休息至少R分钟才能下次继续挤奶.有M次可以挤奶的时间段,每次取奶对应三个值:开始时间.结束时间.效率值,每次挤奶的过程不能中断.求出最大效率值. 解法:首先按照结束时间从小到大排序(按照结束时间排序方便后边的dp):dp[i]表示第i个挤奶时间段后,效率最大值. 转移方程如下: 初始化:dp[i] = data[i].eff #include <iostream> #inclu

POJ 3616 Milking Time(加掩饰的LIS)

传送门: http://poj.org/problem?id=3616 Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13406   Accepted: 5655 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she dec

POJ 3616 Milking Time(最大递增子序列变形)

题目链接:http://poj.org/problem?id=3616 题目大意:给你时间N,还有M个区间每个区间a[i]都有开始时间.结束时间.生产效率(时间都不超过N),只能在给出的时间段内生产,要求合理安排时间求出最大生产价值. 解题思路:把区间按开始时间排序,于是有状态转移方程:dp[i]=max(dp[i],dp[j]+a[i].val)(前提是a[j].end+r<=a[i].start,i是区间的序号,j是i前面的区间) 相当于最大递增子序列的变形,写法差不多. 代码: 1 #in