首页 > 代码库 > POJ3616(KB12-R dp)
POJ3616(KB12-R dp)
Milking Time
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9459 Accepted: 3935
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
Source
1 //2017-05-16 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int M = 1005; 10 int dp[M]; 11 struct Milk 12 { 13 int bg, ed, eff; 14 bool operator<(Milk x) const{ 15 return ed < x.ed; 16 } 17 }milk[M]; 18 19 int main() 20 { 21 int n, m, r; 22 while(scanf("%d%d%d", &n, &m, &r)!=EOF) 23 { 24 memset(dp, 0, sizeof(dp)); 25 for(int i = 0; i < m; i++){ 26 scanf("%d%d%d", &milk[i].bg, &milk[i].ed, &milk[i].eff); 27 } 28 sort(milk, milk+m); 29 int ans = 0; 30 for(int i = 0; i < m; i++){ 31 if(milk[i].ed > n)break; 32 dp[i] = milk[i].eff; 33 for(int j = 0; j < i; j++){ 34 if(milk[i].bg-r >= milk[j].ed){ 35 dp[i] = max(dp[i], dp[j]+milk[i].eff);//dp[i]表示到第i个区间的最大值 36 }else break; 37 } 38 if(dp[i] > ans)ans = dp[i]; 39 } 40 printf("%d\n", ans); 41 } 42 43 return 0; 44 }
POJ3616(KB12-R dp)