描述
http://poj.org/problem?id=3616
給奶牛擠奶,共m次可以擠,給出每次開始擠奶的時間st,結束擠奶的時間ed,還有擠奶的量ef,每次擠完奶要休息r時間,問最大擠奶量.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7507 | Accepted: 3149 |
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
分析
對於每一次擠奶,結束時間+=休息時間.
先把m次擠奶按照開始時間排個序,用f[i]表示擠完第i個時間段的奶以后的最大擠奶量,那么有:
f[i]=max(f[i],f[j]+(第i次擠奶.ef)) (1<=j<i&&(第j次擠奶).ed<=(第i次擠奶).st).
注意:
1.答案不是f[m]而是max(f[i]) (1<=i<=m) (因為不一定最后一次擠奶是哪一次).

1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 5 const int maxm=1005; 6 struct node 7 { 8 int st,ed,ef; 9 bool operator < (const node &a) const 10 { 11 return a.st>st; 12 } 13 }a[maxm]; 14 int n,m,r; 15 int f[maxm]; 16 17 void solve() 18 { 19 for(int i=1;i<=m;i++) 20 { 21 f[i]=a[i].ef; 22 for(int j=1;j<i;j++) 23 { 24 if(a[j].ed<=a[i].st) 25 { 26 f[i]=max(f[i],f[j]+a[i].ef); 27 } 28 29 } 30 } 31 int ans=f[1]; 32 for(int i=2;i<=m;i++) ans=max(ans,f[i]); 33 printf("%d\n",ans); 34 } 35 36 void init() 37 { 38 scanf("%d%d%d",&n,&m,&r); 39 for(int i=1;i<=m;i++) 40 { 41 scanf("%d%d%d",&a[i].st,&a[i].ed,&a[i].ef); 42 a[i].ed+=r; 43 } 44 sort(a+1,a+m+1); 45 } 46 47 int main() 48 { 49 #ifndef ONLINE_JUDGE 50 freopen("milk.in","r",stdin); 51 freopen("milk.out","w",stdout); 52 #endif 53 init(); 54 solve(); 55 #ifndef ONLINE_JUDGE 56 fclose(stdin); 57 fclose(stdout); 58 #endif 59 return 0; 60 }