Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5507 Accepted: 2304
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_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
将每一个区间段按照开始时间从小到大排序,然后动态规划,DP[i]指的是进行到第i个时间段的时候能够得到的最多的产品,这道题有很多坑,
第一个:要将DP初始化,不是0,初始每一段DP的时间数据
第二个:最多的产品数量不一定在最后一个,虽然一般的动态规划题答案都在最后,这道题如果开始时间在中间位置的时候,而结束时间在最后,并且这个时间段内的产品数量又是最多的。。。。这个时候要在每一步加一个变量,始终记录在这个总的时间内的某一个时间段内的最多产值。
第三个也是可有可无的一个:忘记将时间段排序了,思路里记得要排序,码代码的时候漏了,走神了。。。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <memory.h>
using namespace std;
struct time
{
int start;
int end;
int afficiency;
}time[1005];
int dp[1005];
bool cmp(struct time a,struct time b)
{
if(a.start!=b.start)
return a.start<b.start;
return a.end<b.end;
}
int main(void)
{
// freopen("G.txt","r",stdin);
int n,m,r;
while( scanf("%d%d%d",&n,&m,&r)!=EOF)
{
n+=r;
memset(dp,0,sizeof(dp));
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&time[i].start,&time[i].end,&time[i].afficiency);
time[i].end+=r;
}
sort(time+1,time+m+1,cmp);
for(int i=1;i<=m;i++)
dp[i]=time[i].afficiency;
int ans=-1;
for(int i=1;i<=m;i++)
{
for(int j=i+1;j<=m;j++)
{
if(time[i].end<=time[j].start)
dp[j]=max(dp[j],dp[i]+time[j].afficiency);
}
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return 0;
}
467




被折叠的 条评论
为什么被折叠?



