【动态规划】POJ-3616
一、题目
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
二、思路&心得
- 对于”每头奶牛挤奶后需要休息R分钟“的条件限制,直接让每头奶牛的结束时间加上R分钟,来消除这个限制。
- 定义挤奶的结构体(开始时间、结束时间、获得收益),然后按照结束时间从小到大进行排序。
- 这题有点像贪心中的区间问题,排完序后,定义dp[i]为:,到dp[i].end这个时间点为止,可以获得的最大收益。显然,dp[i]满足如下递推式:dp[i] = dp[k] + T[i].gollon。(其中k为dp[0 to i - 1]的最大值下标,且T[k].end <= T[i].start)
- 注意题目不不是在最后一个时间点取到最大值,因此需要记录最大的dp。
三、代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_M = 1005;
int N, M, R;
int dp[MAX_M];
struct times {
int start;
int end;
int gollon;
} T[MAX_M];
bool cmp(times a, times b) {
return a.end < b.end;
}
int main() {
int maxGollons = 0;
scanf("%d %d %d", &N, &M, &R);
for (int i = 0; i <= M; i ++) {
scanf("%d %d %d", &T[i].start, &T[i].end, &T[i].gollon);
T[i].end += R;
}
sort(T, T + M, cmp);
for (int i = 0; i < M; i ++) {
dp[i] = T[i].gollon;
for (int j = 0; j < i; j ++) {
if (T[j].end <= T[i].start) {
dp[i] = max(dp[i], dp[j] + T[i].gollon);
}
}
maxGollons = max(maxGollons, dp[i]);
}
printf("%d\n", maxGollons);
return 0;
}
【动态规划】POJ-3616的更多相关文章
- 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
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...
- poj 3616(动态规划)
Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7265 Accepted: 3043 Desc ...
- 【POJ - 3616】Milking Time(动态规划)
Milking Time 直接翻译了 Descriptions 贝茜是一个勤劳的牛.事实上,她如此专注于最大化她的生产力,于是她决定安排下一个N(1≤N≤1,000,000)小时(方便地标记为0. ...
- 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 1973 Software Company
Software Company Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1112 Accepted: 482 D ...
- poj 3616 Milking Time
Milking ...
随机推荐
- [笔记] FMX 移动平台 TWebBrowser 问题
FMX 移动平台下的 TWebBrowser 有一问题: 某些机子当 WebBrowser.Visible := False; 后,依然留在全屏,虽然看不见,但无法点击操作. 解决:用 WebBrow ...
- python3爬虫-通过selenium获取到dj商品
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...
- Linux—文件命令之touch命令
下面总结一下对于文件的操作命令: satat命令:用于显示文件的详细信息,包括文件.设备.gid.各种时间等. 命令格式:stat filename touch 的两个功能: 1.新建文件,如需建立特 ...
- 《The C Programming Language》读书笔记(一)
1. 对这本书的印象 2011年进入大学本科,C语言入门书籍如果我没记错的话应该是谭浩强的<C程序设计>,而用现在的眼光来看,这本书只能算是一本可用的教材,并不能说是一本好书.在自学操作系 ...
- OSG漫游到指定坐标点位置
OSG中从当前场景位置漫游到指定点坐标位置,osg中场景的视口状态包括如下参数: 1.视点的位置 2.参考点的位置,该点通常为场景中的中心轴上的点 3.视点向上的方向向量 ( const osg::V ...
- python基础学习1-面向对象
#!/usr/bin/env python # -*- coding:utf-8 -*- class Foo:#定义类 def mail(self,email,message):#定义类的方法 pri ...
- 【转载】MFC动态创建控件及其消息响应函数
原文:http://blog.sina.com.cn/s/blog_4a08244901014ok1.html 这几天专门调研了一下MFC中如何动态创建控件及其消息响应函数. 参考帖子如下: (1)h ...
- 33 -jQuery 属性操作,文档操作(未完成)
- 使用web api开发微信公众号,调用图灵机器人接口(二)
此文将分两篇讲解,主要分为以下几步 签名校验; 首次提交验证申请; 接收消息; 被动响应消息(返回XML); 映射图灵消息及微信消息; 此篇为第二篇. 被动响应消息(返回XML) 上一篇中,我们已经可 ...
- Controller组件- 集合点的功能-loadrunner
1.添加集合点功能的做法 ,注意在开始事务前加,不然就会把等待时间也加进去. 2.Controller 中也要开启集合点的功能,才能使用