一、题目

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的更多相关文章

  1. POJ - 3616 Milking Time (动态规划)

    Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...

  2. 动态规划:POJ 3616 Milking Time

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  3. poj 3616(动态规划)

    Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7265   Accepted: 3043 Desc ...

  4. 【POJ - 3616】Milking Time(动态规划)

    Milking Time 直接翻译了 Descriptions 贝茜是一个勤劳的牛.事实上,她如此​​专注于最大化她的生产力,于是她决定安排下一个N(1≤N≤1,000,000)小时(方便地标记为0. ...

  5. POJ 3616 Milking Time(加掩饰的LIS)

    传送门: http://poj.org/problem?id=3616 Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  6. POJ 3616 Milking Time (排序+dp)

    题目链接:http://poj.org/problem?id=3616 有头牛产奶n小时(n<=1000000),但必须在m个时间段内取奶,给定每个时间段的起始时间和结束时间以及取奶质量 且两次 ...

  7. POJ 3616 Milking Time(最大递增子序列变形)

    题目链接:http://poj.org/problem?id=3616 题目大意:给你时间N,还有M个区间每个区间a[i]都有开始时间.结束时间.生产效率(时间都不超过N),只能在给出的时间段内生产, ...

  8. poj 3616 Milking Time (基础dp)

    题目链接 http://poj.org/problem?id=3616 题意:在一个农场里,在长度为N个时间可以挤奶,但只能挤M次,且每挤一次就要休息t分钟: 接下来给m组数据表示挤奶的时间与奶量求最 ...

  9. 二分+动态规划 POJ 1973 Software Company

    Software Company Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1112   Accepted: 482 D ...

  10. poj 3616 Milking Time

                                                                                                 Milking ...

随机推荐

  1. php基础知识考察点:自定义函数及内部函数考察点

    1.变量的作用域和静态变量 函数的参数以及参数的引用传递 函数的返回值以及引用返回 外部文件的导入 系统内置函数的考察 变量的作用域也称为变量的范围,变量的范围即他定义上下文的背景(也是它生效的范围) ...

  2. lua虚拟机概述

    何为虚拟机 用于模拟计算机运行的程序.是个中间层,它处于脚本语言和硬件之间的一个程序.每一门脚本语言都会有自己定义的opcode("操作码"),可以理解为这门程序自己定义的&quo ...

  3. 【转】WCF扩展系列 - 行为扩展(Behaviors)

    原文:https://www.cnblogs.com/Creator/archive/2011/05/21/2052687.html 这个系列的第一部分将会重点关注WCF行为(behaviors),W ...

  4. JQuery第三天——CSS操作与JQuery事件

    JQuery的CSS_DOM与样式操作 样式: 获取 class 和设置 class : class 是元素的一个属性, 所以获取 class 和设置 class 都可以使用 attr() 方法来完成 ...

  5. jdbc动态切换数据库

    http://www.oschina.net/code/snippet_140474_50797

  6. WPF RichTextBox自动调整高度

    原文:WPF RichTextBox自动调整高度 大概两年前的这个时间段,当时做项目遇到了一个问题:环境VS2005.WinForm,需要RichTextBox根据内容自动调整高度.当时用了各种方法都 ...

  7. noip2017普及题解

    https://www.luogu.org/problemnew/show/3954 https://www.luogu.org/problemnew/show/3955 https://www.lu ...

  8. 在云服务器搭建WordPress博客(一)实现云服务器与域名的绑定

    随着云的兴起,越来越多的人选择在云服务器上搭建自己的博客,比较著名的开源博客管理系统当属WordPress了,那么怎么在服务器上搭建WordPress呢? 我们需要让别人能够访问我们的博客,就比如输入 ...

  9. 动态加载与插件系统的初步实现(一):反射与MEF解决方案

    涉及内容: 反射与MEF解决方案 AppDomain卸载与代理 WinForm.WcfRestService示 PRRT1: 反射实现 插件系统的基本目的是实现宿主与组件的隔离,核心是作为接驳约定的接 ...

  10. 记一个小bug的锅

    人生中的第一个线上bug 我参与的第一个项目就出现了.但是自己还觉得这锅也不全是自己的,毕竟那么明显的bug出现在历史模块中(不是我写的新模块),难道测试部就没一点责任?代码走查人员就没一点责任?不过 ...