描述


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

给奶牛挤奶,共m次可以挤,给出每次开始挤奶的时间st,结束挤奶的时间ed,还有挤奶的量ef,每次挤完奶要休息r时间,问最大挤奶量.

Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7507   Accepted: 3149

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

分析


对于每一次挤奶,结束时间+=休息时间.

先把m次挤奶按照开始时间排个序,用f[i]表示挤完第i个时间段的奶以后的最大挤奶量,那么有:

f[i]=max(f[i],f[j]+(第i次挤奶.ef)) (1<=j<i&&(第j次挤奶).ed<=(第i次挤奶).st).

注意:

1.答案不是f[m]而是max(f[i]) (1<=i<=m) (因为不一定最后一次挤奶是哪一次).

 #include<cstdio>
#include<algorithm>
using namespace std; const int maxm=;
struct node
{
int st,ed,ef;
bool operator < (const node &a) const
{
return a.st>st;
}
}a[maxm];
int n,m,r;
int f[maxm]; void solve()
{
for(int i=;i<=m;i++)
{
f[i]=a[i].ef;
for(int j=;j<i;j++)
{
if(a[j].ed<=a[i].st)
{
f[i]=max(f[i],f[j]+a[i].ef);
} }
}
int ans=f[];
for(int i=;i<=m;i++) ans=max(ans,f[i]);
printf("%d\n",ans);
} void init()
{
scanf("%d%d%d",&n,&m,&r);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&a[i].st,&a[i].ed,&a[i].ef);
a[i].ed+=r;
}
sort(a+,a+m+);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("milk.in","r",stdin);
freopen("milk.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}

POJ_3616_Milking_Time_(动态规划)的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. pageContext.request.contextPath

    jsp:<c:set var="ctxStatic" value="${pageContext.request.contextPath}"/>嵌套d ...

  2. Solr使用初探——Solr的安装环境与配置

    Solr是一个apache名下很好用的开源索引.搜索工具,网上的资料虽多但很杂,笔者花了一天的时间对Solr进行了较为初步的研究,对Solr的基础应用做了一定的总结.文中涉及到的配置方法并不唯一,AP ...

  3. java新手笔记13 继承

    1.Person类 package com.yfs.javase; //可以有多个子类 public class Person { private String name;// 私有属性不能继承 pr ...

  4. Flask,HelloWorld

    Flask,HelloWorld # -*- coding:utf-8 -*- ''' Created on 2015年10月19日 ''' from flask import Flask app = ...

  5. (转)C++设计模式——观察者模式

    转自:http://www.jellythink.com/archives/359 前言 之前做了一个性能测试的项目,就是需要对现在的产品进行性能测试,获得测试数据,然后书写测试报告,并提出合理化的改 ...

  6. 第5章标准I/O库总结

    1 fwide函数试图设置流的定向(流的定向决定了读写单字节还是多字节字符) int fwide(FILE *fp,int mode) 宽定向返回正值,字节定向返回负值,为定向返回0 已定向流不会改变 ...

  7. 对称密码-分组密码-AES

    AES产生背景: DES的安全性和应用前景受到挑战,因此需要设计一个高保密性能的.算法公开的.全球免费使用的分组密码算法,用于保护敏感信息,并希望以此新算法取代DES算法,称为新一代数据加密标准,取名 ...

  8. 快速使用Log4Cpp

    封了一下接口,快速使用. 其他的你都不用管了. 这里封装了需要读取外部conf文件配置输出项.否则可以用getInstance初始化日志类 #include "L4Cpp.h" v ...

  9. linux基础之Shell Script入门介绍

    本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下. 本文转自:http://www.jbxue.com ...

  10. 以查询方式实现1s定时

    以查询控制器的控制位状态来实现1s定时. #include <reg52.h> sbit LED = P0^; unsigned ; void main () { LED = ; // 点 ...