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. codeforces Gym 100418D BOPC 打表找规律,求逆元

    BOPCTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  2. Parallax Mapping Shader 凸凹感【转】

    原文 http://www.azure.com.cn/default.asp?cat=11&page=2 Parallax Mapping 就是通过高度图中的高度,对纹理坐标进行偏移,来视觉上 ...

  3. delphi ExecWB

    TWebBrowser.ExecWB 关键点 procedure ExecWB(cmdID: OLECMDID; cmdexecopt: OLECMDEXECOPT); overload; 实现过程 ...

  4. delphi 01设置 字体属性

    设置/获取 字体属性 名称 大小 粗体 斜体 下划线 删除线 颜色1 颜色2   uses MSHTML;   //设置 //------------------------------------- ...

  5. leetcode -- Largest Rectangle in Histogram TODO O(N)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. 网络IPC:套接字之非阻塞和异步I/O

    通常,recv函数没有数据可用时会阻塞等待.同样地,当套接字输出队列没有足够空间来发送消息时函数send会阻塞.在套接字非阻塞模式下,行为会改变.在这种情况下,这些函数不会阻塞而是失败,设置errno ...

  7. C++_直接插入排序(纯C版)

    //用于比较大小 int compared(const void *key1,const void *key2) { cout<<"enter compare"< ...

  8. 利用 Composer 完善自己的 PHP 框架(二)——发送邮件

    本教程示例代码见 https://github.com/johnlui/My-First-Framework-based-on-Composer 回顾 上一篇文章中,我们手工建造了一个简易的视图加载器 ...

  9. Direct3D-3 四元数

        其实本来这篇文章是打算接上篇的各种变化矩阵的推导了,想了想,还是先讲四元数吧.本人的文章并不会提到欧拉角,因为我自己没弄懂欧拉角的万向锁问题.     很多人学习数学时,会有这样一个疑惑,这东 ...

  10. iOS XMPP(2)自己创建客户端

    一.目的以及效果: 用Xcode利用xmpp框架建立客户端,实现向服务器注册添加用户 密码,以及登陆,离线状态 工程的主要结构:新建singleview工程,用xib拖放两个输入框和两个按钮, 并在v ...