POJ 3616 Milking Time dynamic programming method, pojmilking
Description
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her nextN(1 ≤N≤0. 1,000,000) hours (conveniently labeled 0 ..N-1) so that she produces as much milk as possible.
Farmer John has a listM(1 ≤MLess than or equal to 1,000) possibly overlapping intervals in which he is available for milking. Each intervalIHas a starting hour (0 ≤Starting_houri≤N), An ending hour (Starting_houri<Ending_houri≤N), And a corresponding efficiency (1 ≤EfficiencyiLess than or equal to 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 restR(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 inNHours.
Input
* Line 1: Three space-separated integers:N,M, AndR
* Lines 2 ..M+ 1: LineI+ 1 describes FJ's ith milking interval withthree space-separated integers:Starting_houri,Ending_houri, AndEfficiencyi
Output
* Line 1: The maximum number of gallons of milk that Bessie can product inNHours
Sample Input
12 4 21 2 810 12 193 6 247 10 31
Sample Output
43
Source
USACO 2007 November Silver
The task scheduling type is calculated using the dynamic programming method.
Thought 1:
1. sort by task end time
2. Fill in the table and use a one-dimensional table. The value of the table indicates the end time at the current time point. This maximizes the efficiency. Then the state transition equation is obtained: arr [I] = max (arr [I], arr [mt [I]. st] + mt [I]. ef)
Mt [I]. st indicates the start and end times of the current task, and mt [I]. ef indicates the efficiency of the current task.
3. What should I do if the current computing time is not the end time of a task? You can simply enter the maximum efficiency calculated previously.
4. There are also the most important special cases-it is easy to keep up with each other: if the termination time of the two tasks is the same, it is necessary to compare which of the two tasks is more effective, therefore, the Code requires special processing. For more information, see the program.
The following is the implementation function of program Idea 1: int getMaxEfficiency (int N, int M)
Idea 2:
1. the maximum efficiency of the current record is arr [I] = mt [I]. ef.
2. traverse the task j = (0, I) with a shorter end time than the current task I. If the start time and End Time of the current task is equal to or greater than the end time of j, the two tasks can be done together, so the state transition equation is:
Arr [I] = max (arr [I], arr [j] + mt [I]. ef );
3. In this way, each arr [I] represents the maximum efficiency required for the current task to be selected. Therefore, the final result needs to compare the values of all arr.
The implementation function of the following procedure 2 is: int getMaxEffi (int M)
The specific method needs to be determined based on the given data. For example, in this question, we should choose idea 2, because idea 2 saves more memory, because its arr can be opened to the same size as M, the arr of IDEA 1 must be opened at the same size as the N of the question. Therefore, idea 2 saves more memory.
The final procedure is as follows:
# Include <stdio. h> # include <algorithm> # include <limits. h> # include <string. h> using namespace std; const int MAX_N = 1000001; const int MAX_M = 1001; int arr [MAX_N <1]; struct Interval {int st, end, ef; interval (int s = 0, int e1 = 0, int e2 = 0): st (s), end (e1), ef (e2 ){}}; interval mt [MAX_M]; inline int cmp (const void * a, const void * B) {Interval * pa = (Interval *) a; Interval * pb = (Interval *) b; return pa-> end- Pb-> end;} int getMaxEfficiency (int N, int M) {memset (arr, 0, sizeof (int) * (N + 1); if (M) {int I = mt [0]. end; arr [I] = mt [0]. ef ;}for (int I = 1; I <M; I ++) {// special case processing: When two end times are exactly the same, int j = mt [I-1]. end; // If j = mt [I-1]. end + 1 cannot handle special cases int k = mt [I]. end; for (; j + 1 <k; j ++) {arr [j + 1] = arr [j];} arr [k] = max (arr [k], arr [j]); // arr [k] already exists arr [k] = max (arr [k], arr [mt [I]. st] + mt [I]. ef);} return arr [N];} int getMaxEffi (I Nt M) {int maxEf = 0; for (int I = 0; I <M; I ++) {arr [I] = mt [I]. ef; for (int j = 0; j <I; j ++) {if (mt [j]. end <= mt [I]. st) arr [I] = max (arr [I], arr [j] + mt [I]. ef);} maxEf = max (maxEf, arr [I]);} return maxEf;} int main () {int N, M, R, maxN; while (~ Scanf ("% d", & N, & M, & R) {maxN = 0; for (int I = 0; I <M; I ++) {scanf ("% d", & mt [I]. st, & mt [I]. end, & mt [I]. ef); mt [I]. end + = R; if (mt [I]. end> maxN) maxN = mt [I]. end;} qsort (mt, M, sizeof (Interval), cmp); printf ("% d \ n", getMaxEfficiency (maxN, M);} return 0 ;}