Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5607 | Accepted: 2344 |
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
题意:
奶牛Bessie在0~N时间段产奶。农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e。奶牛产奶后需要休息R小时才能继续下一次产奶,求Bessie最大的挤奶量。
定义dp[i]表示第i个时间段挤奶能够得到的最大值,拆开来说,就是前面 i – 1个时间段任取0到i – 1个时间段挤奶,然后加上这个时间段(i)的产奶量之和。dp[i]满足如下递推关系:
第i个时间段挤奶的最大值 = 前 i – 1 个时间段挤奶最大值中的最大值 + 第i次产奶量。
注意此处的第i个时间段不等同于第i次。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
int N,M,R;
int dp[1005];
using namespace std;
struct node
{
int starting_hour,ending_hour,efficiency;
};
node a[1005];
bool cmp(node a,node b)
{
return a.starting_hour < b.starting_hour;
}
int main()
{
cin>>N>>M>>R;
for(int i = 0; i < M; i++)
{
scanf("%d%d%d",&a[i].starting_hour,&a[i].ending_hour,&a[i].efficiency);
a[i].ending_hour += R;
}
sort(a,a+M,cmp);
for(int i = 0; i < M; i++)
{
dp[i] = a[i].efficiency;
for(int j = 0; j < i; j++)
{
if (a[j].ending_hour <= a[i].starting_hour)
dp[i] = max(dp[i],dp[j] + a[i].efficiency);
}
}
cout << *max_element(dp, dp + M) << endl;
}
做题时看到各位巨巨们都说这类题目是水题。。只能说自己的水平不够。。现在定下刷dp的计划就是很好的开始。
希望以后能达到自己想要的水平吧!加油。