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 Nhours

Sample Input

12 4 2
1 2 8
10 12 19
3 6 24
7 10 31

Sample Output

 43



题意:农夫在M个时间段内可以挤奶(起始时间ST,终止时间ET,该时间段内可以获取的奶量C),奶牛每次产奶后需要休息R小时,求在给定时间,求奶牛在这段时间内的最大产奶量。

思路:每次产奶后需要休息R个小时,即挤奶的时间段可视为ST~ET+R。

  1、将区间按时间段的开始时间进行排序。

  2、建立数组s[]表示包含本区间以此区间结尾的区间的最大挤奶量   如 s[3]即为包含有p[3].st~p[3].et区间并以p[3].st~p[3].et结尾的区间的最大挤奶量

  3、s[]的递推公式:  若此区间可与前面的区间相接,则保留这个挤奶量,并最终将这些挤奶量的最大值存于数组是s[]

    s[]=max{可与当前区间相接的前面区间的s[]+当前区间的挤奶量};

  4、遍历数组s[]求数组S中所存的最大值


 #include<cstdio>
#include<algorithm>
using namespace std;
int s[];
struct Node{
int st,et,c;
}p[];
bool cmp( Node a, Node b){
if( a.st<b.st )
return true;
return false;
}
void dp( int m){
for( int i=; i<m; i++){
s[i]=p[i].c;
for( int j=; j<i; j++){
if( p[j].et<=p[i].st )
s[i]=max(s[i],s[j]+p[i].c);
}
}
}
int main()
{
int n,m,r;
int max; while(~scanf("%d%d%d",&n,&m,&r)){
max=-;
for( int i=; i<m; i++){
scanf("%d%d%d",&p[i].st,&p[i].et,&p[i].c);
p[i].et+=r;
}
sort(p,p+m,cmp);
dp(m);
for( int i=; i<m; i++)
if( s[i]>max )
max=s[i];
printf("%d\n",max);
} return ;
}

DP_Milking Time的更多相关文章

随机推荐

  1. java代理,手把手交你写java代理

    一:常用的java代理模式 一般经常做java开发的知道java的代理模式一共有三种,第一种也就是静态代理,这种用法比较简单,没有什么魔法棒,比较好理解,另外两种分别是JDK代理和cglib代理,他们 ...

  2. java安全学习-环境准备/基础知识

    补java的坑,开始! 1.Intellij一些快捷键 intell常用快捷键: ctrl+n 快速查找定位类的位置 ctrl+q 快速查看某个类的文档信息 shift + F6 快速类.变量重命名 ...

  3. matplotlib画图报错This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.

    之前用以下代码将实验结果用matplotlib show出来 plt.plot(np.arange(len(aver_reward_list)), aver_reward_list) plt.ylab ...

  4. MySQL 创建和删除数据表

    创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (column_name col ...

  5. Kotlin 中类函数

    在kotlin中函数可以在类外部定义也可以在类内部定义,前者即为全局函数,后者,是类成员函数,语法一样 package loaderman.demo class Person { fun demo(n ...

  6. hTTP的URL编码

    使用jdk提供的类完成URL的编解码 public class UrlDemo { public static void main(String[] args) throws Exception { ...

  7. JAVA运维总结篇

    写这篇文章主要目的是完成自己多年来运维JAVA应用的一个总结,相当于个人知识库,以后工作中遇到问题便于临时查阅并不断完善自己的知识体系.     上图,就知道Tomcat在JAVA容器界是多么重要. ...

  8. java -- SSM配置完成后,能访问jsp文件不能访问html文件,报错解析

    SSM配置完成后,能访问jsp文件不能访问html文件,报错解析 在确保路径没有任何问题的,情况下,相同的页面,jsp能够正常访问,html却不能正常访问(404). 解决方法: 在web.xml中添 ...

  9. 三种单点登录SSO的实现原理

    单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...

  10. iOS开发之——keychain使用

    iOS的keychain服务提供了一种安全的保存私密信息(密码,序列号,证书等)的方式.每个ios程序都有一个独立的keychain存储.从ios 3.0开始,跨程序分享keychain变得可行. 使 ...