题解Luogu P2889 [USACO07NOV]挤奶的时间Milking Time
Posted 松令
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题解Luogu P2889 [USACO07NOV]挤奶的时间Milking Time相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
题解Luogu2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
luogu P1879 [USACO06NOV]玉米田Corn Fields 题解
BZOJ1229 & 洛谷2917:[USACO2008 NOV]toy 玩具 & 洛谷4480:[BJWC2018]餐巾计划问题——题解