Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14332 | Accepted: 6071 |
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
这次做题主要知道了:一定要明确dp数组的定义再去做题!!!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struct node 7 { 8 int st,et,v; 9 }cow[1002]; 10 int t,n,r,dp[1002]; 11 bool cmp(node a,node b) 12 { 13 if(a.st==b.st) return a.et<b.et; 14 return a.st<b.st; 15 } 16 int main() 17 { 18 scanf("%d%d%d",&t,&n,&r); 19 for(int i=1;i<=n;i++) 20 { 21 scanf("%d%d%d",&cow[i].st,&cow[i].et,&cow[i].v); 22 cow[i].et+=r;//因为考虑间隔,所以直接把区间拉长 23 } 24 sort(cow+1,cow+1+n,cmp); 25 for(int i=1;i<=n;i++) 26 { 27 dp[i]=cow[i].v; 28 for(int j=1;j<i;j++) 29 { 30 if(cow[j].et<=cow[i].st)//首先保证上一个区间处理完了,如果上个区间还没结束,就开始计算这个区间,会重复计算,可以去掉这句用样例看一下 31 dp[i]=max(dp[i],dp[j]+cow[i].v); 32 } 33 } 34 //for(int i=1;i<=n;i++) printf("%d ",dp[i]); 35 printf("%d ",*max_element(dp+1,dp+1+n)); 36 /*选取最大的dp[i],可以试一下这样的样例: 37 100 2 2 38 1 100 99 39 5 6 1 40 就知道为什么了(还可以根据dp定义) 41 */ 42 return 0; 43 }