poj 3616 Milking Time 【dp】
2015-12-20 15:01
309 查看
Milking Time
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: 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
Sample Output
题意:有M个产奶工作,已经给出每个工作的时间范围和获得的牛奶数。在经历一个产奶工作之后,要休息R小时才可以继续下一个工作。问你在[1, N]的时间内可以获得的最大牛奶数。
思路:dp[i]表示前i个产奶工作且第i个产奶工作完成 在[1, N]时间内可以获得的最大牛奶数。
状态转移
dp[i] = max(dp[j]) + num[j].v (1 <= j < i && num[j].t + R <= num[i].s)。
最后维护一下dp[i](1<=i<=M)就可以了。
AC代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6534 | Accepted: 2745 |
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: 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
题意:有M个产奶工作,已经给出每个工作的时间范围和获得的牛奶数。在经历一个产奶工作之后,要休息R小时才可以继续下一个工作。问你在[1, N]的时间内可以获得的最大牛奶数。
思路:dp[i]表示前i个产奶工作且第i个产奶工作完成 在[1, N]时间内可以获得的最大牛奶数。
状态转移
dp[i] = max(dp[j]) + num[j].v (1 <= j < i && num[j].t + R <= num[i].s)。
最后维护一下dp[i](1<=i<=M)就可以了。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #define INF 0x3f3f3f #define eps 1e-8 #define MAXN (1000+1) #define MAXM (100000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; struct Node{ int s, t, v; }; Node num[MAXN]; bool cmp(Node a, Node b){ if(a.s != b.s) return a.s < b.s; else if(a.t != b.t) return a.t < b.t; else return a.v > b.v; } int dp[1010]; int main() { int N, M, R; while(scanf("%d%d%d", &N, &M, &R) != EOF) { for(int i = 1; i <= M; i++) Ri(num[i].s), Ri(num[i].t), Ri(num[i].v); sort(num+1, num+M+1, cmp); CLR(dp, 0); for(int i = 1; i <= M; i++) { dp[i] = num[i].v; for(int j = 1; j < i; j++) if(num[j].t + R <= num[i].s) dp[i] = max(dp[i], dp[j] + num[i].v); } int ans = 0; for(int i = 1; i <= M; i++) ans = max(ans, dp[i]); Pi(ans); } return 0; }
相关文章推荐
- linux 环境变量设置三种方法和查看
- linux学习笔记
- wow使用教程
- 教你3行代码行成高清二维码(Swift1.1)
- 离线储存,application cache,manifest使用体验:
- 利用CDLinux里面的水滴破解路由器密码的教程
- python定时任务调度——apscheduler模块
- 莫尔条纹_20151219
- android EditText获取光标位置并插入字符删除字符
- 推荐系统设计过程中的10条教训
- 使用javax.mail实现邮件的发送
- iOS开发网络篇—多线程断点下载
- 期末总结
- html5离线储存,application cache,manifest使用体验:
- 4.Nexus按项目类型分配不同的工厂来发布不同的项目
- 在VS2013中强制IIS Express应用程序池使用经典模式
- poj 3669 Meteor Shower 【BFS】
- 锐浪报表应用系列四(说一说你不一定知道的取值方法)
- iOS开发网络篇—数据安全
- 浏览器支持与存储限制(请求文件系统)