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

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

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

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

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

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

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

  4. poj 3616 Milking Time (基础dp)

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

  5. poj 3616 Milking Time(dp)

    Description Bessie ≤ N ≤ ,,) hours (conveniently labeled ..N-) so that she produces as much milk as ...

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

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

  7. POJ 3616 Milking Time 简单DP

    题意:奶牛Bessie在0~N时间段产奶.农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e.奶牛产奶后需要休息R小时才能继续下一次产奶,求Bessie最大的挤奶量. 详见代码 ...

  8. POJ 3616 Milking Time (字符串DP)

    题意:找元素关于对角线左或右对称的最大矩阵 思路:左右对角线只需要遍历一条就可以了.只要当前点往上遍历和往后遍历一样就可以. #include<iostream> #include< ...

  9. poj 3616 Milking Time DP

    题意:在给予的N个时间里,奶牛Bessie在M个时间段里进行产奶,但是每次产奶后都要休息R个时间 M个时间段里,分别有开始时间start和结束时间end,和该时间段里产奶的效率efficiency 求 ...

随机推荐

  1. oracle批量导出AWR报告

    工作需求:项目中需要把生产库中所有的AWR报告dump出来,然后导入到方便测试的数据库中.在测试库中的AWR报告需要根据dbid和实例名逐个导出,如果遇到很多再加上RAC系统,会很麻烦.在网上找了一些 ...

  2. seajs 2.3.0 加入jquery

    [前言] 上篇文章简单的介绍了seajs的使用,下午使用seajs整合jquery就碰到问题了. 下载seajs上的examples,里面直接require('jquery')没有不论什么问题, 我照 ...

  3. AES加密算法(C++实现,附源代码)

    先搞定AES算法,基本变换包含SubBytes(字节替代).ShiftRows(行移位).MixColumns(列混淆).AddRoundKey(轮密钥加) 其算法一般描写叙述为 明文及密钥的组织排列 ...

  4. 利用mmap /dev/mem 读写Linux内存

    转载:http://blog.csdn.net/zhanglei4214/article/details/6653568 使用 hexedit /dev/mem 可以显示所有物理内存中的信息. 运用m ...

  5. CentOS 安装中文输入法

    转载:http://blog.sina.com.cn/s/blog_9f1c093101019h03.html centos 6.3用yum安装中文输入法 1.需要root权限,所以要用root登录 ...

  6. 琐碎-关于hadoop2.2.0

    HDFS模块功能 namenode:主节点,存储文件的元数据如文件名.文件目录结构.文件属性(生成时间.副本数.文件权限).以及每个文件的块列表和块所在的datanode等: datanode:在本地 ...

  7. css笔记11:选择器练习

    1. (1)exam1.css文件: .s1 { font-size: 50px; color: blue; } .s2 { backgoround:gray; font-style: italic; ...

  8. iOS “获取验证码”按钮的倒计时功能

    iOS 的倒计时有多种实现细节,Cocoa Touch 为我们提供了 NSTimer 类和 GCD 的dispatch_source_set_timer方法去更加方便的使用计时器.我们也可以很容易的的 ...

  9. CF Watto and Mechanism (字典树+深搜)

    Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  10. [改善Java代码]覆写equals方法必须覆写hashCode方法

    覆写equals方法必须覆写hashCode方法,这条规则基本上每个Javaer都知道,这也是JDK API上反复说明的,不过为什么要这样做呢?这两个方法之间有什么关系呢?本建议就来解释该问题,我们先 ...