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\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_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

USACO 2007 November Silver

时间: 2024-10-24 07:29:42

POJ 3616 Milking Time 简单DP的相关文章

POJ 3616 Milking Time (排序+dp)

题目链接:http://poj.org/problem?id=3616 有头牛产奶n小时(n<=1000000),但必须在m个时间段内取奶,给定每个时间段的起始时间和结束时间以及取奶质量 且两次取奶之间须间隔r-1个小时,求最大取奶质量 也就是说r = 2时 3分结束取奶,至少在5分才能取. 按照时间排序,dp[i]表示i时段的最大产奶量 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include &

POJ 3616 Milking Time 基础DP

Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5743   Accepted: 2401 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 ≤

poj 3616 Milking Time(dp)

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. Fa

POJ 3616 Milking Time DP题解

典型的给出区间任务和效益值,然后求最大效益值的任务取法. 属于一维DP了. 一维table记录的数据含义:到当前任务的截止时间前的最大效益值是多少. 注意, 这表示当前任务一定要选择,但是最终结果是不一定选择最后一个任务,故此最后需要遍历找到table数组的最大值,当然计算过程中使用一个数记录最终最大值也是可以的. 状态转移方程就是: tbl[i] = MAX({from tbl[0]->tbl[i-1] }+ weight[i] ),即区间0到i-1加上i的当前效益值. #include <

POJ 3616 Milking Time 挤奶问题,带权区间DP

题目链接:POJ 3616 Milking Time Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4837   Accepted: 2034 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to sc

[2016-03-28][POJ][3616][Milking Time]

时间:2016-03-28 17:27:03 星期一 题目编号:[2016-03-28][POJ][3616][Milking Time] #include <algorithm> #include <cstdio> using namespace std; const int maxm = 1000 + 10; struct Roo{ int l,r,v; bool operator < (const Roo & a)const{ return l < a.l

poj 3616 Milking Time dp+树状数组

题意: 给一堆区间,每个区间有开始时间s,结束时间e,和收益w,现在要找一些区间使收益和最大,且区间之间的间隔最小为r. 分析: 这道题用dp做是简单题,用dp+树状数组做是中等题.dp问题的关键是对状态的定义.有两种方法,一:dp[k]表示按开始时间排序到第k个区间能取得的最大收益.二:dp[t]表示在时间t时能获得的最大收益.定义好状态方程就好写了这不再赘述.有趣的是这个时间复杂度.设一共有M个区间,所有区间的最大时间为L,第一种是M^2的,第二种是M*(logL+logM)的,这题M才10

POJ 3616 Milking Time(加掩饰的LIS)

传送门: http://poj.org/problem?id=3616 Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13406   Accepted: 5655 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she dec

POJ 3616 Milking Time

简单DP,感觉就是最长上升子序列的变形 #include<cstdio> #include<cstring> #include<cmath> #include<stack> #include<vector> #include<string> #include<iostream> #include<algorithm> using namespace std; const int maxn=1000+10; st