POJ3616 Milking Time【dp】
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
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
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
思路:首先按照結束時間排序,dp[i]表示i時間擠奶的最大量,那麼dp[i] = dp[i-1] + i時間擠奶量,仔細看程式碼推一推應該就會理解了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1000005;
const int M=1005;
int a[N],dp[N];
struct milk
{
int start,end,value;
}s[M];
bool cmp(milk x,milk y)
{
return x.end<y.end;
}
int main()
{
int n,m,r;
scanf("%d%d%d",&n,&m,&r);
for(int i=1;i<=m;++i)
scanf("%d%d%d",&s[i].start,&s[i].end,&s[i].value);
sort(s+1,s+m+1,cmp);
memset(dp,0,sizeof(dp));
int maxn=0;
for(int i=1;i<=m;++i)
{
for(int j=1;j<i;++j)
{
if(s[j].end+r<=s[i].start)
dp[i]=max(dp[i],dp[j]);
}
dp[i]+=s[i].value;
maxn=max(maxn,dp[i]);
}
printf("%d\n",maxn);
return 0;
}