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
题目大意:Bessie在她接下来的N小时内尽可能挤出更多的奶,给出一些挤奶的时间片和对应的奶产量(Ei),每次挤完奶后需要休息R小时,求最多能挤出多少奶。
先按照时间片的结束时间升序排序,设C(i)代表以i为结尾选择中的时间片最大量产奶,C(i)= max{C(k) + Ei, 0<= k < i},上代码。#ifndef HEAD #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string> #include <iostream> #include <queue> #include <list> #include <algorithm> #include <stack> #include <map> using namespace std; #endif // !HEAD struct MT { int start; int end; int value; }; int MTARRAY[1001]; bool mtcomp(MT& a1, MT& a2) { return a1.end < a2.end; } int main() { #ifdef _DEBUG freopen("d:\\in.txt", "r", stdin); #endif int n, m,r; memset(MTARRAY, 0, sizeof(MTARRAY)); scanf("%d %d %d\n", &n, &m, &r); vector<MT> mts; for (int i = 0; i < m;i++) { MT mt; scanf("%d %d %d\n", &mt.start, &mt.end, &mt.value); mt.end += r; mts.push_back(mt); } sort(mts.begin(), mts.end(), mtcomp); MTARRAY[0] = mts[0].value; int max = 0; for (int i = 1; i < m;i++) { MTARRAY[i] = mts[i].value; for (int k = 0; k < m;k++) { if (mts[k].end <= mts[i].start) { MTARRAY[i] =std:: max(MTARRAY[i], MTARRAY[k] + mts[i].value); max = std::max(max, MTARRAY[i]); } else { break; } } } printf("%d\n", max); return 0; }