Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6599 | Accepted: 2764 |
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 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 8 const int maxn = 1e6 + 7; 9 typedef long long ll; 10 int n, m, r; 11 ll dp[maxn][2]; 12 struct node { 13 int s, t, e; 14 bool operator < (const node &rhs) const { 15 return s < rhs.s; 16 } 17 }; 18 19 node a[1005]; 20 21 int main() { 22 23 // freopen("sw.in", "r", stdin); 24 scanf("%d%d%d", &n, &m, &r); 25 for (int i = 0; i < m; ++i) { 26 scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].e); 27 } 28 29 30 sort(a, a + m); 31 memset(dp, -1, sizeof(dp)); 32 dp[0][1] = dp[0][0] = 0; 33 for (int i = 0; i < n; ++i) { 34 for (int st = 0; st <= 1; ++st) { 35 if (st == 0) { 36 if (i + r <= n) 37 dp[i + r][1] = max(dp[i][st], dp[i + r][1]); 38 } else { 39 dp[i + 1][1] = max(dp[i][st], dp[i + 1][1]); 40 int id = lower_bound(a, a + m, (node) {i, 0, 0}) - a; 41 while (id < m && a[id].s == i) { 42 dp[ a[id].t ][0] = max(dp[ a[id].t ][0], dp[i][st] + a[id].e); 43 id++; 44 } 45 } 46 } 47 } 48 ll ans = 0; 49 for (int i = 0; i <= n; ++i) { 50 //printf("dp[%d][0] = %d dp[%d][1] = %d ", i, dp[i][0], i, dp[i][1]); 51 ans = max(ans, dp[i][0]); 52 ans = max(ans, dp[i][1]); 53 } 54 55 cout << ans << endl; 56 // 57 // for (int i = 0; i < m; ++i) { 58 // printf("%d %d %d ", a[i].s, a[i].t, a[i].e); 59 // } 60 61 return 0; 62 }
上述的方法虽然可行但其实想复杂了,只需要设dp[i]为到i时间的最大效率值
dp[i] = max(dp[i - 1], dp[ a[k].s - r ]+ a[k].e)即可
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 8 const int maxn = 1e6 + 7; 9 typedef long long ll; 10 int n, m, r; 11 ll dp[maxn]; 12 struct node { 13 int s, t, e; 14 bool operator < (const node &rhs) const { 15 return t < rhs.t; 16 } 17 }; 18 19 node a[1005]; 20 21 int main() { 22 23 //freopen("sw.in", "r", stdin); 24 scanf("%d%d%d", &n, &m, &r); 25 for (int i = 0; i < m; ++i) { 26 scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].e); 27 } 28 29 30 sort(a, a + m); 31 memset(dp, 0, sizeof(dp)); 32 33 int k = 0; 34 for (int T = 1; T <= n; ++T) { 35 dp[T] = max(dp[T], dp[T - 1]); 36 while (k < m && T >= a[k].t) { 37 if (T != a[k].t) continue; 38 if (a[k].s - r < 0) { 39 dp[T] = max(dp[T], (ll)a[k].e); 40 } else { 41 dp[T] = max(dp[T], dp[ a[k].s - r] + a[k].e); 42 43 } 44 k++; 45 } 46 } 47 48 ll ans = 0; 49 for (int i = 0; i <= n; ++i) ans = max(ans, dp[i]); 50 cout << ans << endl; 51 52 // 53 // for (int i = 0; i < m; ++i) { 54 // printf("%d %d %d ", a[i].s, a[i].t, a[i].e); 55 // } 56 57 return 0; 58 }
其实还有一种最简单的做法,把区间按开始时间升序排列,如果开始时间相同则按结束时间,设dp[i]为从开始到第i的区间段的最大值
dp[i] = max(dp[i], dp[j] + a[k].e);
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 8 const int maxn = 1005; 9 typedef long long ll; 10 int n, m, r; 11 ll dp[maxn]; 12 struct node { 13 int s, t, e; 14 bool operator < (const node &rhs) const { 15 return s < rhs.s || (s == rhs.s && t < rhs.t) ; 16 } 17 }; 18 19 node a[1005]; 20 21 int main() { 22 23 //freopen("sw.in", "r", stdin); 24 scanf("%d%d%d", &n, &m, &r); 25 for (int i = 0; i < m; ++i) { 26 scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].e); 27 } 28 29 30 sort(a, a + m); 31 memset(dp, 0, sizeof(dp)); 32 for (int i = 0; i < m; ++i) dp[i] = a[i].e; 33 34 for (int i = 1; i < m; ++i) { 35 for (int j = 0; j < i; ++j) { 36 if (a[j].t + r <= a[i].s) { 37 dp[i] = max(dp[i], dp[j] + a[i].e); 38 } 39 } 40 } 41 42 ll ans = 0; 43 for (int i = 0; i < m; ++i) { 44 ans = max(ans, dp[i]); 45 } 46 47 cout << ans << endl; 48 49 // 50 // for (int i = 0; i < m; ++i) { 51 // printf("%d %d %d ", a[i].s, a[i].t, a[i].e); 52 // } 53 54 return 0; 55 }