一、题目

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. 使用SSH命令从一台Linux远程登陆到另一台Linux

    命令格式: ssh 用户名@IP 示例: ssh root@192.168.1.10 回车,然后根据提示输入登陆密码即可.

  2. 实现点击到底部、顶部、指定div功能

    顶部: $(".back_top").click(function () { scrollTo(0, 0); }); function goTop() { $('html, bod ...

  3. scala_类的继承

    Scala继承一个基类跟Java很相似, 但我们需要注意以下几点: 重写一个非抽象方法必须使用override修饰符,以及重写父类属性也必须使用override修饰符. 只有主构造函数才可以往基类的构 ...

  4. WPF触控方面的技术点

    一.基本的触控事件(原始触控) 二.复杂触控事件(操作)

  5. JavaWeb基础—JS学习小结

    JavaScript是一种运行在浏览器中的解释型的编程语言 推荐:菜鸟教程一.简介js:javascript是基于对象[哪些基本对象呢]和和事件驱动[哪些主要事件呢]的语言,应用在客户端(注意与面向对 ...

  6. BZOJ1190_梦幻岛宝珠_KEY

    题目传送门 观察数据a*2^b,转化成二进制后,后面跟了b位的0,可以转化为一个分层背包. 先预处理出每个物品是哪一层的,并放在同层内DP. 同层内直接背包,考虑层与层之间的DP. 第一维枚举层数,然 ...

  7. 联赛emacs配置

    (custom-set-variables ;; custom-set-variables was added by Custom. ;; If you edit it by hand, you co ...

  8. css布局笔记(三)圣杯布局,双飞翼布局

    圣杯布局和双飞翼布局都是三列布局,两边定宽,中间自适应布局,中间栏要在放在文档流前面以优先渲染. 圣杯布局如下 <!-- 圣杯布局 --> <!DOCTYPE html> &l ...

  9. Vue随性小笔记

    1 前端MVC 和 后端MVC不同: 可以看出前端MVC其实为了解决前端复杂js模块化的问题,从后端MVC的V分离出来的 2     MVC / MVP / MVVM 三者区别  Model View ...

  10. python属性访问

    1.python属性访问魔法方法: >>> class C: def __getattribute__(self,name): print("getattribute&qu ...