POJ 3616 Milking Time 動態規劃法題解,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 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 21 2 810 12 193 6 247 10 31
Sample Output
43
Source
USACO 2007 November Silver
任務安排類型的動態規劃法計算。
思路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;}