传送门:

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. Coursera 机器学习 第5章 Neural Networks: Learning 学习笔记

    5.1节 Cost Function神经网络的代价函数. 上图回顾神经网络中的一些概念: L  神经网络的总层数. sl  第l层的单元数量(不包括偏差单元). 2类分类问题:二元分类和多元分类. 上 ...

  2. Linux服务器性能评估与优化--转

    http://www.itlearner.com/article/4553 一.影响Linux服务器性能的因素 1. 操作系统级 Ø       CPU Ø       内存 Ø       磁盘I/ ...

  3. git代码管理工具-SourceTree 使用介绍

    一.SourceTree 简单说明 SourceTree 是git 代码管理的可视化工具,可省去操作命令行的一个图形化工具,下载地址:https://www.sourcetreeapp.com/ 二. ...

  4. C# 服务端控件 asp:RadioButton 选择选中值

    1.服务端控件RadioButton <asp:RadioButton ID="rbNewUser" runat="server" GroupName=& ...

  5. JavaScript各类型变量和对象

    一.javascript支持的数据类型: var x=1 数字 var x=0.1   小数 var x=true/false bool var x="abc"      字符串 ...

  6. Xcode10 闪退问题

    最新更新了iOS12,mac10.13.6,xcode10之后,打开之前的项目,只要进行import,xcode就会闪退.那么就来看一下解决方案: Xcode10 新增了一个构建系统起名“New Bu ...

  7. My eclipse jdk unbound的解决

    project --> properties --> java build path --> 双击出错的jdk --> alternate jre --> install ...

  8. 支持触屏的jQuery轮播图插件

    移动轮播图我看到两类, 一款是无线天猫的m.tmall.com和携程,实现了无缝轮播. 一款是蘑菇街的,没有实现无缝轮播. 我自己重写一个,类似天猫. 1.页面代码 <!DOCTYPE html ...

  9. 前端(三大框架、Bootstrap,jQuery,自整理)

    前端,HTML(超文本标记语言),CSS(层叠样式表)和JavaScript(脚本语言) HTML,通常说的h5,其实按标准来说,HTML4的后续版本不带编号了,并保证向前的兼容性 CSS的版本3,增 ...

  10. 微信小程序的button去边框

    wxml <button class='niu'>123123</button> css niu::after{ border:none; }