一、题目

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. MySQL学习【第六篇sql语句下】

    一.select高级用法 1.传统连接(只能内连接,取交集,效率最慢) 1.根据两张表查询张三成绩 select t1.sname,t2.mark from t1,t2 where t1.sid=t2 ...

  2. 在线调整InnoDB Buffer Pool Size

    InnoDB Buffer Pool主要是用来缓存数据表和索引数据的内存区域,它的默认值为134217728字节(128MB).最大值取决于CPU架构;32位系统上的最大值为4294967295(23 ...

  3. React Webpack cookbook

    https://christianalfoni.github.io/react-webpack-cookbook/index.html https://fakefish.github.io/react ...

  4. 20155211 2016-2017-2 《Java程序设计》第九周学习总结

    20155211 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 整合数据库 一.JDBC入门 (一)JDBC简介 厂商在操作JDBC驱动程序时,依 ...

  5. python基础学习1-三元表达式和lambda表达式

    #!/usr/bin/env python # -*- coding:utf-8 -*- 三元运算 if else 的简写 name ="alex" if 1==1 else &q ...

  6. python基础学习1 -异常捕获

    #!/usr/bin/env python # -*- coding:utf-8 -*- #-------try-except try: file_name = input("请输入需要打开 ...

  7. C++实现tar包解析

    tar(tape archive)是Unix和类Unix系统上文件打包工具,可以将多个文件合并为一个文件,使用tar工具打出来的包称为tar包.一般打包后的文件名后缀为".tar" ...

  8. lua编程之元表与元方法

    一. 前言 lua是一种非常轻量的动态类型语言,在1993年由由Roberto Ierusalimschy.Waldemar Celes 和 Luiz Henrique de Figueiredo等人 ...

  9. MySQL5.7(二)数据库的基本操作

    登录MySQL数据库 格式:mysql -u 用户名 -h 主机名或IP地址  -P 端口号 -p 密码

  10. 【SIKIA计划】_07_Unity3D游戏开发-坦克大战笔记

    [新增分类][AudioClips]音频剪辑[AudioMixers]音频混合器[Editor][Fonts]字体[Materials]材质[Models]模型[Standard Assets] [渲 ...