Luogu P2889 [USACO07NOV]挤奶的时间Milking Time

题目描述

传送门
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.

奶牛Bessie在0~N时间段产奶。农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e。奶牛产奶后需要休息R小时才能继续下一次产奶,求Bessie最大的挤奶量。

输入输出格式

输入格式:

  • 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

输出格式:

  • Line 1: The maximum number of gallons of milk that Bessie can product in the N hours

输入输出样例

输入样例#1: 复制

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

输出样例#1: 复制

43

思路

  • 可以发现从挤奶开始一直到休息完是一个整体,都不能进行转移

  • 所以直接将挤奶时间看作一个整体变成 $time+r$ 进行转移就可以了

  • $f[i]$表示第i分钟的最优收益,$time[i]$ 为这次挤奶的持续时间, $w[i]$ 是这次挤奶的收益

  • 转移方程 $f[i+time[i]+r]=max(f[i+time[i]+r],f[i]+w[i])$

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define re register int
using namespace std;
int read(){
int x=0,w=1; char ch=getchar();
while(ch!='-'&&(ch<'0'&&ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*w;
}
int n,m,r,f[2000005],y;
bool tmp[2000005];
struct data{
int st,tim,w;
}cow[1005];
bool cmp(data cow,data b){
return cow.st<b.st;
}
int main(){
//freopen("p2889.in","r",stdin);
//freopen("p2889.out","w",stdout);
n=read(),m=read(),r=read();
n=n+r+10;
for(re i=1;i<=m;i++){
cow[i].st=read(),y=read(),cow[i].w=read();
cow[i].tim=y-cow[i].st+r;
tmp[cow[i].st]=1;
}
sort(cow+1,cow+1+m,cmp);
int j=1;
for(re i=0;i<=n;i++){
f[i]=max(f[i],f[i-1]);
if(tmp[i]){
for(;cow[j].st==i;j++){
f[i+cow[j].tim]=max(f[i+cow[j].tim],f[i]+cow[j].w);
}
}
}
printf("%d\n",f[n]);
return 0;
}

【题解】Luogu P2889 [USACO07NOV]挤奶的时间Milking Time的更多相关文章

  1. P2889 [USACO07NOV]挤奶的时间Milking Time

    P2889 [USACO07NOV]挤奶的时间Milking Time 奶牛Bessie在0~N时间段产奶.农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e.奶牛产奶后需要休息 ...

  2. bzoj1642 / P2889 [USACO07NOV]挤奶的时间Milking Time

    P2889 [USACO07NOV]挤奶的时间Milking Time 普通的dp 休息时间R其实就是把结束时间后移R个单位而已.但是终点也需要后移R位到n+R. 每个时间段按起始时间排序,蓝后跑一遍 ...

  3. [USACO07NOV]挤奶的时间Milking Time

    https://daniu.luogu.org/problemnew/show/2889 按右端点从小到大排序后DP dp[i] 到第i个时间段的最大产奶量 不能按左端点排序,第i段由第j段更新时,第 ...

  4. 题解 最优的挤奶方案(Optimal Milking)

    最优的挤奶方案(Optimal Milking) 时间限制: 1 Sec  内存限制: 128 MB 题目描述 农场主 John 将他的 K(1≤K≤30)个挤奶器运到牧场,在那里有 C(1≤C≤20 ...

  5. [题解] Luogu P5446 [THUPC2018]绿绿和串串

    [题解] Luogu P5446 [THUPC2018]绿绿和串串 ·题目大意 定义一个翻转操作\(f(S_n)\),表示对于一个字符串\(S_n\), 有\(f(S)= \{S_1,S_2,..., ...

  6. 【题解】Luogu P1204 [USACO1.2]挤牛奶Milking Cows

    原题传送门:P1204 [USACO1.2]挤牛奶Milking Cows 实际是道很弱智的题目qaq 但窝还是觉得用珂朵莉树写会++rp(窝都初二了,还要考pj) 前置芝士:珂朵莉树 窝博客里对珂朵 ...

  7. 题解 Luogu P2499: [SDOI2012]象棋

    关于这道题, 我们可以发现移动顺序不会改变答案, 具体来说, 我们有以下引理成立: 对于一个移动过程中的任意一个移动, 若其到达的位置上有一个棋子, 则该方案要么不能将所有棋子移动到最终位置, 要么可 ...

  8. 题解 luogu P1144 【最短路计数】

    本蒟蒻也来发一次题解第一篇请见谅 这个题有几个要点 1.无向无权图,建图的时候别忘记建来回的有向边[因此WA掉1次 2.无权嘛,那么边长建成1就好了2333333 3.最短路采用迪杰斯特拉(别忘用堆优 ...

  9. [LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays

    https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离 ...

随机推荐

  1. Jenkins 基础篇 - 安装部署

    Jenkins 安装 Jenkins 支持主流的 Linux 发行版系统,同时还支持 macOS.Windows.和 Docker 运行. 具体系统的 Jenkins 安装包可以去官网下载 https ...

  2. 一款好用的数据血缘关系在线工具--SQLFlow

      l  数据血缘关系(data lineage) 数据血缘属于数据治理中的一个概念,是在数据溯源的过程中找到相关数据之间的联系,它是一个逻辑概念.数据治理中经常提到血缘分析,血缘分析是保证数据融合的 ...

  3. Java虚拟机栈和PC寄存器

    PC Register介绍 JVM中的程序计数寄存器(Program Counter Register)中,Register 的命名源于CPU的寄存器,寄存器存储指令相关的现场信息.CPU只有把数据装 ...

  4. OAuth2.0 授权方式及步骤梳理总结

    OAuth 2.0授权协议使第三方应用程序可以通过协调资源所有者和HTTP服务之间的批准交互,或者通过允许第三方应用程序代表资源所有者来获得对HTTP服务的有限访问权,或者代表资源所有者. 代表自己获 ...

  5. 风变编程(Python自学笔记)第10关-工作量计算器

    1.%f的意思是格式化字符串为浮点型,%.1f的意思是格式化字符串为浮点型,并保留1位小数. 2.向上取整:ceil() 使用ceil()方法时需要导入math模块,例如 1 >>> ...

  6. [刷题] PTA 查验身份证

    题目: 7-63 查验身份证 (15 分)  一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5, ...

  7. Jira/Confluence配置Apache SSL 证书

    1. 申请证书 生成私钥 openssl genrsa -out server.key 2048 生成request文件 openssl req -new -key server.key -out s ...

  8. 命令stat anaconda-ks.cfg会显示出文件的三种时间状态(已加粗):Access、Modify、Change。这三种时间的区别将在下面的touch命令中详细详解:

    7.stat命令 stat命令用于查看文件的具体存储信息和时间等信息,格式为"stat 文件名称". stat命令可以用于查看文件的存储信息和时间等信息,命令stat anacon ...

  9. shell 读取某个目录下的所有文件

    #!/bin/shFILE_PATH="xxx" xxx:路径cd $FILE_PATHfor FILE in `ls` do echo $FILE done

  10. 下面附上top和sar的使用方法,方便参考! "top"工具

    下面附上top和sar的使用方法,方便参考! "top"工具 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示 ...