传送门:

http://poj.org/problem?id=3616

Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13406   Accepted: 5655

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_houriN), an ending hour (starting_houri < ending_houriN), 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 ≤ RN) 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

Source

 
题目意思:
奶牛为自己规划下面n时间内的产奶,m个时间段,每个段有a,b,c表示从a时到b时共可产奶c。
挤奶工每次挤奶必须挤完完整的时间段,且每次挤完需要休息r时,求最终可获得的牛奶最大值
 
做法:
按结束时间(=结束时间+时间休息)排个序 贪心的思想,为什么是按照结束时间排序,具体请参考贪心经典样例之活动安排问题
为什么:是为了剩余时间最大化
 
    先按照贪心的想法按照结束时间(输入的结束时间+R)升序排序,
    然后用动态规划做。
    dp[i]代表第i个时间区间挤奶可获得的最大产奶量,
    需要遍历前i-1个时间区间中结束时间小于等于第i个时间区间中开始时间,
    求出最大值加上第i个时间区间的产奶量就是dp[i],
    最后去dp数组最大值即可。
 
code:
 
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define max_v 1005
struct node
{
int s,f,v;
}p[max_v];
bool cmp(node a,node b)
{
return a.f<b.f;
}
int dp[max_v];
int main()
{
/*
先按照贪心的想法按照结束时间(输入的结束时间+R)升序排序,
然后用动态规划做。
dp[i]代表第i个时间区间挤奶可获得的最大产奶量,
需要遍历前i-1个时间区间中结束时间小于等于第i个时间区间中开始时间,
求出最大值加上第i个时间区间的产奶量就是dp[i],
最后去dp数组最大值即可。
*/
int n,m,r;
while(cin>>n>>m>>r)
{
for(int i=;i<m;i++)
{
scanf("%d %d %d",&p[i].s,&p[i].f,&p[i].v);
p[i].f+=r;
}
sort(p,p+m,cmp);
for(int i=;i<max_v;i++)
dp[i]=;
dp[]=p[].v;
int ans=dp[];
for(int i=;i<m;i++)
{
int t=;
for(int j=;j<i;j++)
{
if(p[j].f<=p[i].s)
{
t=max(t,dp[j]);
}
}
dp[i]=t+p[i].v;
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return ;
}

POJ 3616 Milking Time(加掩饰的LIS)的更多相关文章

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

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

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

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

  3. poj 3616 Milking Time (基础dp)

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

  4. poj 3616 Milking Time

                                                                                                 Milking ...

  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. 《springcloud 四》服务保护机制

    服务保护机制SpringCloud Hystrix 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用 ...

  2. Basic Data Structures and Algorithms in the Linux Kernel--reference

    http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...

  3. HttpServletRequest的获取客户端真实IP

    摘自:http://chenyoulu.diandian.com/post/2012-11-14/40042540378 request方法客户端IP: request.getRemoteAddr() ...

  4. APNS 证书生成注意事项

    APNS证书导出pem: openssl x509 -in aps_development.cer -inform der -out yourCertName.pem APNS证书密钥导出: 先在&q ...

  5. Use the list and while to Build Shop car

    #Author: Gordonsalary = int(input("请输入你的工资:"))goods = [('0',"Iphone",5000),('1', ...

  6. typeScript入门(一)构建环境和数据类型

    最近入坑v-cli 3.0,发现ts越来越常用了,于是开始入坑学习. 1.构建ts环境 npm install -g typescript Mac和vscode用户可以用以下方式构建tsdemo项目 ...

  7. Intellij idea配置scala开发环境

    1.Intellij idea配置scala开发环境 解决Plugin Scala was not installed: No route to host Plugin Scala was not i ...

  8. cropperjs的高度过大(container height too much)

    cropperjs的高度过大(container height too much) 标签(空格分隔): JavaScript 业务需要web头像裁切,用canvas写了个demo卡成一匹马,于是就去寻 ...

  9. YUV数据详解

    http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html YUV格式有两大类:planar和packed.对于planar的YUV ...

  10. HttpClient拉取连载小说

    上午刚入手的小说,下午心血来潮想从网站上拉取下来做成电子书,呵呵,瞎折腾-说做就做- [抓包] 这一步比什么都重要,如果找不到获取真正资源的那个请求,就什么都不用做了- 先是打算用迅雷把所有页面都下载 ...