POJ 3616 Milking Time 简单DP

题目链接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
", 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_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: NM, 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

原文地址:https://www.cnblogs.com/bolderic/p/7375305.html