Milking Time
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 5
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.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <string> 5 #include <cstdio> 6 using namespace std; 7 long long dp[1000005]; 8 struct node 9 { 10 long long s, e, c; 11 }; 12 bool cmp(node x, node y) 13 { 14 if (x.e == y.e) 15 { 16 if (x.c == y.c) 17 { 18 return x.s < y.s; 19 } 20 return x.c > y.c; 21 } 22 return x.e < y.e; 23 } 24 node a[1000005];//这里也要开这么大,不知道为什么呢 25 int main() 26 { 27 long long n, m, r; 28 cin >> n >> m >> r; 29 long long i; 30 for (i = 1; i <= m; i++) 31 { 32 cin >> a[i].s >> a[i].e >> a[i].c; 33 a[i].e = a[i].e + r; 34 } 35 sort(a + 1, a + 1 + m, cmp); 36 memset(dp, 0, sizeof(dp)); 37 long long j; 38 i = 1; 39 for (j = 1;j <= n+r; j++) 40 { 41 dp[j] = dp[j - 1]; 42 if (i<=m) 43 { 44 if (j == a[i].e) 45 { 46 while (a[i].e == j) 47 { 48 dp[j] = max(dp[j], dp[a[i].s] + a[i].c); 49 i++; 50 if (i > m) break; 51 } 52 } 53 } 54 } 55 ////for (int i = 1; i <= m; i++) 56 ////{ 57 //// dp[i] = a[i].c; 58 //// for (int j = 1; j<i; j++) 59 //// { 60 //// if (a[j].e <= a[i].s) 61 //// { 62 //// dp[i] = max(dp[i], dp[j] + a[i].c); 63 //// } 64 65 //// } 66 ////} 67 long long mm = 0; 68 for (i = 1; i <= m; i++) 69 { 70 if (dp[a[i].e] > mm) mm = dp[a[i].e]; 71 } 72 cout << mm << endl; 73 return 0; 74 }
对于每一次挤奶,结束时间+=休息时间.
先把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) (因为不一定最后一次挤奶是哪一次).
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <string> 5 using namespace std; 6 int dp[1000005]; 7 struct node 8 { 9 int s, e, c; 10 }; 11 bool cmp(node x, node y) 12 { 13 if (x.e == y.e) 14 { 15 if (x.c == y.c) 16 return x.s < y.s; 17 return x.c > y.c; 18 } 19 return x.e < y.e; 20 } 21 int main() 22 { 23 int n, m, r; 24 cin >>n >> m >> r; 25 node a[1005]; 26 int i; 27 for (i = 1; i <= m; i++) 28 { 29 cin >> a[i].s >> a[i].e >> a[i].c; 30 a[i].e = a[i].e + r; 31 } 32 sort(a + 1, a + 1 + m, cmp); 33 memset(dp, 0, sizeof(dp)); 34 int j; 35 i = 1; 36 //for (j = 1;j <= n; j++) 37 //{ 38 // if (i<=m&&j == a[i].e) 39 // { 40 // while (i<=m&&a[i].e == j) 41 // { 42 // if (a[i].s - r < 0) 43 // { 44 // dp[j] = max(a[i].c, dp[a[i].e]); 45 // } 46 // dp[j] = max(dp[j], dp[a[i].s - r] + a[i].c); 47 // i++; 48 // } 49 // } 50 // else 51 // { 52 // dp[j] = dp[j - 1]; 53 // } 54 //} 55 for (int i = 1; i <= m; i++) 56 { 57 dp[i] = a[i].c; 58 for (int j = 1; j<i; j++) 59 { 60 if (a[j].e <= a[i].s) 61 { 62 dp[i] = max(dp[i], dp[j] + a[i].c); 63 } 64 65 } 66 } 67 int ans = dp[1]; 68 for (int i = 2; i <= m; i++) ans = max(ans,dp[i]); 69 cout << ans << endl; 70 //cout << dp[n] << endl; 71 return 0; 72 }