1. 程式人生 > >POJ 3616 Milking Time 簡單DP

POJ 3616 Milking Time 簡單DP

++ cti als mission i++ mit wid ble turn

題目鏈接:http://poj.org/problem?id=3616

題目大意:M個區間,每個區間一個對應一個效率值-多少升牛奶,區間可能重復,現要求取出來一些區間,要求是區間間隔不能小於R,問所能得到的牛奶量的最大值。

解題思路:決策:當前區間用或者不用。區間個數M≤1000,因此直接雙循環遞推即可。

dp[i]:=選第i個區間情況下前i個區間能獲得的牛奶最大值

dp[i] = max(dp[i], dp[j] + a[i].eff)  a[j].end + r <= a[i].start

代碼:

 1 const int inf = 0x3f3f3f3f;
 2 const int maxn = 1e3 + 5
; 3 struct node{ 4 int st, ed, eff; 5 bool operator < (const node &t) const{ 6 if(st != t.st) return st < t.st; 7 else return ed < t.ed; 8 } 9 }; 10 node a[maxn]; 11 int n, m, r; 12 int dp[maxn]; 13 14 int cmp(node x, node y){ 15 if(x.st != y.st) return
x.st < y.st; 16 else return x.ed < y.ed; 17 } 18 void solve(){ 19 sort(a + 1, a + m + 1); 20 memset(dp, 0, sizeof(dp)); 21 22 for(int i = 1; i <= m; i++){ 23 dp[i] = a[i].eff; 24 for(int j = i - 1; j >= 0; j--){ 25 if(a[j].ed + r <= a[i].st){
26 dp[i] = max(dp[i], dp[j] + a[i].eff); 27 } 28 } 29 } 30 int ans = 0; 31 for(int i = 1; i <= m; i++) ans = max(ans, dp[i]); 32 printf("%d\n", ans); 33 } 34 int main(){ 35 scanf("%d %d %d", &n, &m, &r); 36 for(int i = 1; i <= m; i++) 37 scanf("%d %d %d", &a[i].st, &a[i].ed, &a[i].eff); 38 solve(); 39 }

題目:

Milking Time
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10444 Accepted: 4380

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_houriN), an ending hour (starting_houri < ending_houriN), 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 ≤ RN) 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

USACO 2007 November Silver

POJ 3616 Milking Time 簡單DP