poj 3616 Milking Time
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8048 | Accepted: 3388 |
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 题意:在[1,N]的区间上,包含许多子区间,在每个子区间里农夫约翰可以给牛挤奶,每段子区间牛产奶量也有区别,并且一旦在某个区间挤了奶,牛都要休息一定时间才能继续挤奶.共有M个挤奶区间,求最多能挤多少奶。
思路:动态规划,dp[i]意义:到第i个挤奶区间为止能挤到的最多的牛奶量。
任取整数k属于[1,i-1],每个区间k的end时间都小于区间i的开始时间,那么到第i个挤奶区间为止能挤到的最多的牛奶量等于max{第k个区间为止能挤到的最多的牛奶量+第i个区间能挤到的牛奶量},即dp[i]=max{dp[k]+interval[i].efficiency},若找不到任何一个k,dp[i]=interval[i].efficiency
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
using namespace std;
const int N_MAX = ;
struct interval {
int begin, end, efficiency;
bool operator <(const interval&b)const {
return begin < b.begin;
}
};
interval Interval[N_MAX];
int dp[N_MAX];//dp[i]代表i个区间及之前的区间牛生产牛奶的最大值
int main() {
int N,M,R;
while (cin >> N>>M>>R) {
for (int i = ;i < M;i++) {
scanf("%d%d%d", &Interval[i].begin, &Interval[i].end, &Interval[i].efficiency);
Interval[i].end += R;//每个区间结束时间相当于加上休息时间
}
sort(Interval,Interval+M);
for (int i = ;i < M;i++) {
dp[i] = Interval[i].efficiency;
for (int j = ;j < i;j++) {
if (Interval[j].end<= Interval[i].begin) {
dp[i] = max(dp[i], dp[j] + Interval[i].efficiency);//前面(i-1)个区间中任意一个和当前区间不重叠的区间k
} //dp[i]就是所有dp[k]+Interval[i].efficiency中的最大值
}
} cout << *max_element(dp, dp + M) << endl;
}
return ;
}
poj 3616 Milking Time的更多相关文章
- POJ 3616 Milking Time(加掩饰的LIS)
传送门: http://poj.org/problem?id=3616 Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ 3616 Milking Time (排序+dp)
题目链接:http://poj.org/problem?id=3616 有头牛产奶n小时(n<=1000000),但必须在m个时间段内取奶,给定每个时间段的起始时间和结束时间以及取奶质量 且两次 ...
- POJ 3616 Milking Time(最大递增子序列变形)
题目链接:http://poj.org/problem?id=3616 题目大意:给你时间N,还有M个区间每个区间a[i]都有开始时间.结束时间.生产效率(时间都不超过N),只能在给出的时间段内生产, ...
- poj 3616 Milking Time (基础dp)
题目链接 http://poj.org/problem?id=3616 题意:在一个农场里,在长度为N个时间可以挤奶,但只能挤M次,且每挤一次就要休息t分钟: 接下来给m组数据表示挤奶的时间与奶量求最 ...
- poj 3616 Milking Time(dp)
Description Bessie ≤ N ≤ ,,) hours (conveniently labeled ..N-) so that she produces as much milk as ...
- POJ - 3616 Milking Time (动态规划)
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...
- POJ 3616 Milking Time 简单DP
题意:奶牛Bessie在0~N时间段产奶.农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e.奶牛产奶后需要休息R小时才能继续下一次产奶,求Bessie最大的挤奶量. 详见代码 ...
- POJ 3616 Milking Time (字符串DP)
题意:找元素关于对角线左或右对称的最大矩阵 思路:左右对角线只需要遍历一条就可以了.只要当前点往上遍历和往后遍历一样就可以. #include<iostream> #include< ...
- poj 3616 Milking Time DP
题意:在给予的N个时间里,奶牛Bessie在M个时间段里进行产奶,但是每次产奶后都要休息R个时间 M个时间段里,分别有开始时间start和结束时间end,和该时间段里产奶的效率efficiency 求 ...
随机推荐
- Codeforces Gym 100286A. Aerodynamics 计算几何 求二维凸包面积
Problem A. AerodynamicsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...
- 使用C# 生成word记录
private void button1_Click(object sender, System.EventArgs e) { object oMissing = System.Reflection. ...
- 【Android 应用开发】Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
19af543b068bdb7f27787c2bc69aba7f Additional Download (32-, 64-bit) Package r10 STL debug info androi ...
- delphi 获取 TreeView选中的文件路径
//获取 TreeView选中的文件路径 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, G ...
- android 退出整个程序
上网学了好多方法,关于Android的Activity如何退出.好多方法都不行啦,试了各种方法,下面这种方法就是我最喜欢的,简单又容易懂. 使用单例模式创建一个Activity管理对象,该对象中有一个 ...
- int a[5]={}, &a+1与(int*)a+1的区别
#include <iostream> #include <typeinfo> using namespace std; int main() { int b, *pb; ch ...
- Online Schema Upgrade in MySQL Galera Cluster using TOI Method
http://severalnines.com/blog/online-schema-upgrade-mysql-galera-cluster-using-toi-method As a fo ...
- QFtp类参考
QFtp是一个用来实现FTP协议的类. 详情请见…… #include <qftp.h> 继承了QNetworkProtocol. 所有成员函数的列表. 公有成员 QFtp () virt ...
- do {...} while (0) in macros
If you are a C programmer, you must be familiar with macros. They are powerful and can help you ease ...
- [转] 详解http和https的作用与区别
PS: https就是http和TCP之间有一层SSL层,这一层的实际作用是防止钓鱼和加密.防止钓鱼通过网站的证书,网站必须有CA证书,证书类似于一个解密的签名.另外是加密,加密需要一个密钥交换算法, ...