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 <algorithm>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <ctime>
10 #include <list>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long LL;
15 typedef pair <int, int> P;
16 const int N = 1e3 + 5;
17 struct data {
18     int l, r, val;
19     bool operator <(const data& cmp) const {
20         return l < cmp.l;
21     }
22 }a[N];
23 int dp[N];
24
25 int main()
26 {
27     int n, m, r;
28     while(~scanf("%d %d %d", &n, &m, &r)) {
29         memset(dp, 0, sizeof(dp));
30         for(int i = 1; i <= m; ++i) {
31             scanf("%d %d %d", &a[i].l, &a[i].r, &a[i].val);
32         }
33         sort(a + 1, a + m + 1);
34         int res = 0;
35         for(int i = 1; i <= m; ++i) {
36             dp[i] = a[i].val;
37             for(int j = 1; j < i; ++j) {
38                 if(a[i].l - a[j].r >= r) {
39                     dp[i] = max(dp[i], dp[j] + a[i].val);
40                 }
41             }
42             res = max(res, dp[i]);
43         }
44         printf("%d\n", res);
45     }
46     return 0;
47 }
时间: 2024-08-07 16:37:22

POJ 3616 Milking Time (排序+dp)的相关文章

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 基础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

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

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

[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

题意:在给予的N个时间里,奶牛Bessie在M个时间段里进行产奶,但是每次产奶后都要休息R个时间 M个时间段里,分别有开始时间start和结束时间end,和该时间段里产奶的效率efficiency 求问,应该如何选择哪些时间段进行产奶,才能使得效率最大化 我的错误做法:设D[i]为到时间i以内,所能够产奶的最大量,从而d[i] = max(d[i-1], d[st[k]]+ef[k]); 最终还是TLE了,由于N最大为1000000,且k最大为1000,所以综合还是太花费时间了 正确思路:我看到