Milking Time
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_houri ≤ N), an ending hour (starting_houri < ending_houri ≤ N), 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 ≤ R ≤ N) 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
/* 题意:奶牛在0~n的区间内产奶,农场主有m个时间段可以挤奶并且给出第i个时间段的挤奶产量,农场主每次挤完奶之后必须休息时间r之后 才能工作,问农场主最多可以挤多少奶 初步思路:贪心 #错误:贪心解决不了问题还是得动态规划,dp[i]表示第i个时间段能获得的最大奶量,dp[i]=max(dp[i],dp[j]+fr[i].val); 注意这个dp[j]的结束时间不能和dp[i]的开始时间冲突 */ #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; struct node{ int l,r,val; bool operator < (const node & other) const{ if(l==other.l) return r<other.r; return l<other.l; } }fr[1005]; int dp[1005]; int m,n,r; int maxn; void init(){ memset(dp,0,sizeof dp); maxn=-1; } int main(){ // freopen("in.txt","r",stdin); while(scanf("%d%d%d",&n,&m,&r)!=EOF){ init(); for(int i=1;i<=m;i++){ scanf("%d%d%d",&fr[i].l,&fr[i].r,&fr[i].val); fr[i].r+=r; } sort(fr+1,fr+m+1); for(int i=1;i<=m;i++){ dp[i]=fr[i].val; for(int j=1;j<i;j++){ if(fr[j].r<=fr[i].l){ dp[i]=max(dp[i],dp[j]+fr[i].val); } } maxn=max(dp[i],maxn); } printf("%d\n",maxn); } return 0; }