Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3607 | Accepted: 1533 |
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 21 2 810 12 193 6 247 10 31
Sample Output
43
动态规划问题,用一个数组dp[i]记录到第i小时最大的产奶量
先将所有牛的产奶时间按结束时间由小到大排序
然后将i从0到n遍历递推一遍,每次先将dp[i]初始化为dp[i-1],然后遍历所有结束时间与i相等的奶牛
dp[i]=max(dp[i],dp[time[t].start_hour-r]+time[t].eff)
注意time[t].start_hour-r<0时,递推式是这样的:
dp[i]=max(dp[i],time[t].eff)
1 #include2 #include 3 #include 4 #include 5 6 using namespace std; 7 8 typedef struct 9 {10 int start_hour;11 int end_hour;12 int eff;13 } INTERVAL;14 15 int n,m,r;16 int dp[1000010];17 INTERVAL time[1010];18 19 bool cmp(INTERVAL a,INTERVAL b)20 {21 return a.end_hour