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. python 学习笔记re

    在学习python的过程中很多时候都需要用到re(正则),因为我也不是开发所以呢只是简单说一下经常需要用到的东西. 在工作中经常用到的主要是三个函数:1.math   2.search   3.sub ...

  2. Asp.Net 之 通过调用 WScript.Shell 启动本地 exe 程序时产生“ automation服务器不能创建对象 ”的错误

    我们经常需要通过生成 ActiveXObject("WScript.Shell"); 来调用某一exe文件. 设置网页打印的页眉页脚为空: var HKEY_Root,HKEY_P ...

  3. SpringMVC的简单示例

    首先导入所需的jar包,项目目录结构如下: 之后需要配置一下web.xml文件,内容如下: <?xml version="1.0" encoding="UTF-8& ...

  4. 通过java实现对数据库的增删改查

    package cn.hncu; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet; ...

  5. 纯JS操作服务器绑定控件(Repeat)实现表头升降排序

    JS实现功能 var obj = function (id) { return "string" == typeof id ? document.getElementById(id ...

  6. css笔记02:选择器(标签式和类)

    body { margin:; padding:; background:#000 url('images/backgrounds/star.png') no-repeat fixed; font: ...

  7. CentOS(七)--Linux文件类型及目录配置

    这篇随笔将会对Linux系统的文件类型以及Linux的目录结构进行详细补充(linux中目录管理和权限非常重要,特别是在linux安装数据库类软件). 一.Linux更改文件权限的两种方式 在之前的一 ...

  8. 给jdk写注释系列之jdk1.6容器(13)-总结篇之Java集合与数据结构

         是的,这篇blogs是一个总结篇,最开始的时候我提到过,对于java容器或集合的学习也可以看做是对数据结构的学习与应用.在前面我们分析了很多的java容器,也接触了好多种常用的数据结构,今天 ...

  9. Oracle procedure 基本语法

    转自:http://lorry1113.javaeye.com/blog/513851 关键字: oracle 存储过程 1.基本结构 CREATE OR REPLACE PROCEDURE 存储过程 ...

  10. Scala中的类和对象

    类的定义 使用class定义 类的字段 在类中使用var,val定义字段 类的方法 scala中,使用var定义字段默认提供setter和getter方法对应名称为 value_= 和value /* ...