首页 > 代码库 > POJ 3616 Milking Time 动态规划法题解
POJ 3616 Milking Time 动态规划法题解
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 , andefficiencyi
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:
1 按照任务的结束时间排序
2 填表,使用一维表即可,表的值表示以当前时间点为结束时间,得到的最大效率。那么就得到状态转换方程:arr[i] = max (arr[i], arr[mt[i].st]+mt[i].ef)
其中mt[i].st代表当前任务的起止时间,mt[i].ef代表当前任务的效率。
3 那么当当前计算的时间点不是某任务的结束时间,改如何处理呢?可以直接把之前计算得到的最大效率填上来就可以。
4 还有最重要的处理特殊情况-很容易栽跟斗的地方: 如果两个任务的终止时间是一样的,那么就需要比较这两个任务选择哪个比较有效,故此代码就需要特殊处理,具体看程序。
下面程序思路1的实现函数是:int getMaxEfficiency(int N, int M)
思路2:
1 记录当前的最大效率为arr[i] = mt[i].ef
2 遍历比当前任务i的终止时间更小的任务j = (0, i),如果当前任务的起止适合大于等于j的终止时间,那么这两个任务就可以一起做,故此状态转换方程为:
arr[i] = max(arr[i], arr[j]+mt[i].ef);
3 这样每个arr[i]就代表当前任务必须要选择的时候得到的最大效率,故此最终结果需要比较所有arr的值。
下面程序思路2的实现函数是:int getMaxEffi(int M)
具体选用哪种方法需要根据所给的数据而定,比如本题就应该选择思路2,因为思路2更加省内存,因为其arr可以开得和M一样大小,而思路1的arr必须开得和题目的N一样大小,故此思路2更加省内存。
最终程序如下:
#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++) {//特殊情况处理:两个end时间完全一样的时候 int j = mt[i-1].end;//如果是j=mt[i-1].end+1就无法处理特殊情况 int k = mt[i].end; for (; j+1 < k; j++) { arr[j+1] = arr[j]; } arr[k] = max(arr[k], arr[j]);//arr[k]已经存在 arr[k] = max(arr[k], arr[mt[i].st]+mt[i].ef); } return arr[N]; } int getMaxEffi(int 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 %d %d", &N, &M, &R)) { maxN = 0; for (int i = 0; i < M; i++) { scanf("%d %d %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; }
POJ 3616 Milking Time 动态规划法题解