Description
Input
Output
Sample Input
Sample Output
Hint
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
题意:
一个奶牛在0~N时间段内可被取奶,每次挤奶以后必须休息至少R分钟才能下次继续挤奶。有M次可以挤奶的时间段,每次取奶对应三个值:开始时间、结束时间、效率值,每次挤奶的过程不能中断。求出最大效率值。
解法:
首先按照结束时间从小到大排序(按照结束时间排序方便后边的dp);dp[i]表示第i个挤奶时间段后,效率最大值。
代码实现:
#include <iostream> #include <cstdio> #include <queue> #include <vector> #include <map> #include <cmath> #include <algorithm> using namespace std; struct node // 记录每次挤奶的参数; { int sta; int End; int val; }; bool cmp( node a, node b ) { return a.End < b.End; } int main() { int N, M, R; cin >> N >> M >> R; node m[M]; int i, j; for( i=0; i<M; i++ ) { cin >> m[i].sta >> m[i].End >> m[i].val; } sort( m, m+M, cmp ); // 根据每次挤奶的结束时间排序; int dp[M]; int sum = 0; // 最大奶量; for( i=0; i<M; i++ ) { dp[i] = m[i].val; for( j=0; j<i; j++) { if( m[j].End+R <= m[i].sta ) // 如果在本次挤奶之前还能挤奶,则到这次挤奶最大奶量为
// max(本次奶量, 本次奶量 + 之前最大奶量); dp[i] = max( dp[i], m[i].val + dp[j] ); } if( dp[i] > sum ) sum = dp[i]; } cout << sum << endl; return 0; }