POJ_3616_Milking_Time_(动态规划)
描述
http://poj.org/problem?id=3616
给奶牛挤奶,共m次可以挤,给出每次开始挤奶的时间st,结束挤奶的时间ed,还有挤奶的量ef,每次挤完奶要休息r时间,问最大挤奶量.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7507 | Accepted: 3149 |
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
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
Sample Output
43
Source
分析
对于每一次挤奶,结束时间+=休息时间.
先把m次挤奶按照开始时间排个序,用f[i]表示挤完第i个时间段的奶以后的最大挤奶量,那么有:
f[i]=max(f[i],f[j]+(第i次挤奶.ef)) (1<=j<i&&(第j次挤奶).ed<=(第i次挤奶).st).
注意:
1.答案不是f[m]而是max(f[i]) (1<=i<=m) (因为不一定最后一次挤奶是哪一次).
#include<cstdio>
#include<algorithm>
using namespace std; const int maxm=;
struct node
{
int st,ed,ef;
bool operator < (const node &a) const
{
return a.st>st;
}
}a[maxm];
int n,m,r;
int f[maxm]; void solve()
{
for(int i=;i<=m;i++)
{
f[i]=a[i].ef;
for(int j=;j<i;j++)
{
if(a[j].ed<=a[i].st)
{
f[i]=max(f[i],f[j]+a[i].ef);
} }
}
int ans=f[];
for(int i=;i<=m;i++) ans=max(ans,f[i]);
printf("%d\n",ans);
} void init()
{
scanf("%d%d%d",&n,&m,&r);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&a[i].st,&a[i].ed,&a[i].ef);
a[i].ed+=r;
}
sort(a+,a+m+);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("milk.in","r",stdin);
freopen("milk.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
POJ_3616_Milking_Time_(动态规划)的更多相关文章
- 增强学习(三)----- MDP的动态规划解法
上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- C#动态规划查找两个字符串最大子串
//动态规划查找两个字符串最大子串 public static string lcs(string word1, string word2) { ...
- C#递归、动态规划计算斐波那契数列
//递归 public static long recurFib(int num) { if (num < 2) ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
随机推荐
- MinGW-notepad++开发c/c++程序
下载MinGW 点击下载 安装好后运行 最后点击左上角的 Installation,开始安装 1.编译: g++ -o a.exe a.cpp gcc -o hello.exe hello.c 2.运 ...
- 00_ForTest
-----该页是爬虫的测试页请忽略------- 1234545@qq.comadasdsdasdsad阿打算多少其热情为 asdasdasdasd4w5wsdvv啊实打实大 啊实打实大asdasda ...
- 洛谷 U3178 zty的冒险之行
U3178 zty的冒险之行 题目提供者mangoyang 题目背景 "妈咪妈咪轰"随着一声巨响,zty传送到了Aluba国,在那里浴血奋战,饱读兵书,风餐露宿,吃喝嫖赌,终于到了 ...
- c# 与 winform 界面开发
在 windows 下使用 vs2010 开发,未深入研究. c# 与 .net 开发,一堆又一堆的新名词,头晕目眩,比如 CLR / apartments / STA / MTA / COM 吐槽无 ...
- Java---Hibernate>>Can't create table './xxx/#sql-b2c_1a.frm' (errno: xxx)解决方法
通用方案:删除相关表,重新生成. 1.关联表之间数据引擎不一致导致: 修改相关表的引擎设定,保持一致. 2.关联表索引字段的引用类型不一样(如A表关联字段是int,B表索引是char): 修改相关表的 ...
- [开发环境] Ubuntu12.04 Telnet服务设置
Ubuntu12.04默认未启用Telnet服务,配置Telnet服务步骤如下: 1. 安装和配置 安装openbsd-inetd: #sudo apt-get install openbsd-ine ...
- wdcp日志
apache或nginx都有开关默认日志,一个是正常访问日志,一个是错误的日志,目录在 /www/wdlinux/nginx-1.0.15/logs /www/wdlinux/httpd-2.2.22 ...
- linux下安装php扩展redis缓存
下载phpredis安装包 wget https://github.com/nicolasff/phpredis/tarball/master 在下载目录解压phpredis.tar.gz tar z ...
- VS2015 C#.net4.6 windows的定时服务
1.创建项目 C#->经典桌面->windows服务 2.创建定时服务 找到服务1,自动生成了 OnStart(服务开启时运行),OnStop(服务关闭时运行)两个函数,我们需要在开启时新 ...
- 简单的介绍下WPF中的MVVM框架
最近在研究学习Swift,苹果希望它迅速取代复杂的Objective-C开发,引发了一大堆热潮去学它,放眼望去各个培训机构都已打着Swift开发0基础快速上手的招牌了.不过我觉得,等同于无C++基础上 ...