poj 3616

Milking Time

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6599   Accepted: 2764

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

有三种设状态的方法

  第一种设dp[i][0]为时间到i时刚刚停止尚未休息的最大效率,dp[i][1]为时间到i时已经经过足够的时间休息的最大值,则

不喂奶,让时间流走一分钟 dp[i + 1][1] = max(dp[i + 1][1], dp[i][1]);

  二分找到开始时间等于i的所有区间 dp[ a[k].t ][0] = max(dp[ a[k].t ][0], dp[i][1] + a[k].e);

  休息r时间 dp[i + r][1] = max(dp[i + r][1], dp[i][0]);

  

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5
 6 using namespace std;
 7
 8 const int maxn = 1e6 + 7;
 9 typedef long long ll;
10 int n, m, r;
11 ll dp[maxn][2];
12 struct node  {
13     int s, t, e;
14     bool operator < (const node &rhs) const {
15         return s < rhs.s;
16     }
17 };
18
19 node a[1005];
20
21 int main() {
22
23    // freopen("sw.in", "r", stdin);
24     scanf("%d%d%d", &n, &m, &r);
25     for (int i = 0; i < m; ++i) {
26         scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].e);
27     }
28
29
30     sort(a, a + m);
31     memset(dp, -1, sizeof(dp));
32     dp[0][1] = dp[0][0] = 0;
33     for (int i = 0; i < n; ++i) {
34         for (int st = 0; st <= 1; ++st) {
35             if (st == 0) {
36                 if (i + r <= n)
37                 dp[i + r][1] = max(dp[i][st], dp[i + r][1]);
38             } else {
39                 dp[i + 1][1] = max(dp[i][st], dp[i + 1][1]);
40                 int id = lower_bound(a, a + m, (node) {i, 0, 0}) - a;
41                 while (id < m && a[id].s == i) {
42                     dp[ a[id].t ][0] = max(dp[ a[id].t ][0], dp[i][st] + a[id].e);
43                     id++;
44                 }
45             }
46         }
47     }
48     ll ans = 0;
49     for (int i = 0; i <= n; ++i) {
50         //printf("dp[%d][0] = %d dp[%d][1] = %d \n", i, dp[i][0], i, dp[i][1]);
51         ans = max(ans, dp[i][0]);
52         ans = max(ans, dp[i][1]);
53     }
54
55     cout << ans << endl;
56 //
57 //    for (int i = 0; i < m; ++i) {
58 //        printf("%d %d %d\n", a[i].s, a[i].t, a[i].e);
59 //    }
60
61     return 0;
62 }

  上述的方法虽然可行但其实想复杂了,只需要设dp[i]为到i时间的最大效率值

  dp[i] = max(dp[i - 1], dp[ a[k].s - r ]+ a[k].e)即可

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5
 6 using namespace std;
 7
 8 const int maxn = 1e6 + 7;
 9 typedef long long ll;
10 int n, m, r;
11 ll dp[maxn];
12 struct node  {
13     int s, t, e;
14     bool operator < (const node &rhs) const {
15         return t < rhs.t;
16     }
17 };
18
19 node a[1005];
20
21 int main() {
22
23     //freopen("sw.in", "r", stdin);
24     scanf("%d%d%d", &n, &m, &r);
25     for (int i = 0; i < m; ++i) {
26         scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].e);
27     }
28
29
30     sort(a, a + m);
31     memset(dp, 0, sizeof(dp));
32
33     int k = 0;
34     for (int T = 1; T <= n; ++T) {
35         dp[T] = max(dp[T], dp[T - 1]);
36         while (k < m && T >= a[k].t) {
37             if (T != a[k].t) continue;
38             if (a[k].s - r < 0) {
39                 dp[T] = max(dp[T], (ll)a[k].e);
40             } else {
41                 dp[T] = max(dp[T], dp[ a[k].s - r] + a[k].e);
42
43             }
44             k++;
45         }
46     }
47
48     ll ans = 0;
49     for (int i = 0; i <= n; ++i) ans = max(ans, dp[i]);
50     cout << ans << endl;
51
52 //
53 //    for (int i = 0; i < m; ++i) {
54 //        printf("%d %d %d\n", a[i].s, a[i].t, a[i].e);
55 //    }
56
57     return 0;
58 }

其实还有一种最简单的做法,把区间按开始时间升序排列,如果开始时间相同则按结束时间,设dp[i]为从开始到第i的区间段的最大值

  dp[i] = max(dp[i], dp[j] + a[k].e);

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5
 6 using namespace std;
 7
 8 const int maxn = 1005;
 9 typedef long long ll;
10 int n, m, r;
11 ll dp[maxn];
12 struct node  {
13     int s, t, e;
14     bool operator < (const node &rhs) const {
15         return s < rhs.s || (s == rhs.s && t < rhs.t) ;
16     }
17 };
18
19 node a[1005];
20
21 int main() {
22
23     //freopen("sw.in", "r", stdin);
24     scanf("%d%d%d", &n, &m, &r);
25     for (int i = 0; i < m; ++i) {
26         scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].e);
27     }
28
29
30     sort(a, a + m);
31     memset(dp, 0, sizeof(dp));
32     for (int i = 0; i < m; ++i) dp[i] = a[i].e;
33
34     for (int i = 1; i < m; ++i) {
35         for (int j = 0; j < i; ++j) {
36             if (a[j].t + r <= a[i].s) {
37                 dp[i] = max(dp[i], dp[j] + a[i].e);
38             }
39         }
40     }
41
42     ll ans = 0;
43     for (int i = 0; i < m; ++i) {
44         ans = max(ans, dp[i]);
45     }
46
47     cout << ans << endl;
48
49 //
50 //    for (int i = 0; i < m; ++i) {
51 //        printf("%d %d %d\n", a[i].s, a[i].t, a[i].e);
52 //    }
53
54     return 0;
55 }

时间: 2024-10-13 22:27:40

poj 3616的相关文章

[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题解

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

R - Milking Time POJ 3616 ( DP )

R - Milking Time Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3616 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedul

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 挤奶问题,带权区间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

POJ 3616 Milking Time(最大递增子序列变形)

题目链接:http://poj.org/problem?id=3616 题目大意:给你时间N,还有M个区间每个区间a[i]都有开始时间.结束时间.生产效率(时间都不超过N),只能在给出的时间段内生产,要求合理安排时间求出最大生产价值. 解题思路:把区间按开始时间排序,于是有状态转移方程:dp[i]=max(dp[i],dp[j]+a[i].val)(前提是a[j].end+r<=a[i].start,i是区间的序号,j是i前面的区间) 相当于最大递增子序列的变形,写法差不多. 代码: 1 #in

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

题目链接: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 代

POJ 3616 Milking Time 动态规划

http://poj.org/problem?id=3616 题意:一个奶牛在0~N时间段内可被取奶,每次挤奶以后必须休息至少R分钟才能下次继续挤奶.有M次可以挤奶的时间段,每次取奶对应三个值:开始时间.结束时间.效率值,每次挤奶的过程不能中断.求出最大效率值. 解法:首先按照结束时间从小到大排序(按照结束时间排序方便后边的dp):dp[i]表示第i个挤奶时间段后,效率最大值. 转移方程如下: 初始化:dp[i] = data[i].eff #include <iostream> #inclu

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