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: 复制

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

输出样例#1: 复制

  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])$

代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #define re register int
  6. using namespace std;
  7. int read(){
  8. int x=0,w=1; char ch=getchar();
  9. while(ch!='-'&&(ch<'0'&&ch>'9')) ch=getchar();
  10. if(ch=='-') w=-1,ch=getchar();
  11. while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
  12. return x*w;
  13. }
  14. int n,m,r,f[2000005],y;
  15. bool tmp[2000005];
  16. struct data{
  17. int st,tim,w;
  18. }cow[1005];
  19. bool cmp(data cow,data b){
  20. return cow.st<b.st;
  21. }
  22. int main(){
  23. //freopen("p2889.in","r",stdin);
  24. //freopen("p2889.out","w",stdout);
  25. n=read(),m=read(),r=read();
  26. n=n+r+10;
  27. for(re i=1;i<=m;i++){
  28. cow[i].st=read(),y=read(),cow[i].w=read();
  29. cow[i].tim=y-cow[i].st+r;
  30. tmp[cow[i].st]=1;
  31. }
  32. sort(cow+1,cow+1+m,cmp);
  33. int j=1;
  34. for(re i=0;i<=n;i++){
  35. f[i]=max(f[i],f[i-1]);
  36. if(tmp[i]){
  37. for(;cow[j].st==i;j++){
  38. f[i+cow[j].tim]=max(f[i+cow[j].tim],f[i]+cow[j].w);
  39. }
  40. }
  41. }
  42. printf("%d\n",f[n]);
  43. return 0;
  44. }

【题解】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. mybatis增删改返回的int是-2147482646,并不是想要返回结果

    MyBatis发现更新和插入返回值一直为"-2147482646"的错误是由defaultExecutorType设置引起的,如果设置为batch,更新返回值就会丢失,返回结果就只 ...

  2. Postman报文进行解密之RSA私钥解密

    接口返回的数据也是加密的,需要对数据解密才能看到返回的数据是否正确,就需要用RSA解密. 返回数据的解析可以在postman的Tests进行后置处理,获取加密后的返回数据: var data = JS ...

  3. 【js】Leetcode每日一题-停在原地的方案数

    [js]Leetcode每日一题-停在原地的方案数 [题目描述] 有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处. 每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指 ...

  4. Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2)

    A. Prison Break 题意:就是在一个n*m的矩阵中,以(1,1)为起点(n,m)为终点,每个点以每个单位1s的速度移动,问总共至少需要多少秒,所有的矩阵点就能够全部移动到(r,c)中 思路 ...

  5. C++ primer plus读书笔记——第8章 函数探幽

    第8章 函数探幽 1. 对于内联函数,编译器将使用相应的函数代码替换函数调用,程序无需跳到一个位置执行代码,再调回来.因此,内联函数的运行速度比常规函数稍快,但代价是需要占用更多内存. 2. 要使用内 ...

  6. 普里姆算法(Prim)邻接矩阵法

    算法代码 C#代码 using System; namespace Prim { class Program { static void Main(string[] args) { int numbe ...

  7. Linux进阶之日志管理

    一.何为日志 1.在程序执行时,可以通过标准输出以及错误输出,让我们知道程序的执行情况,而系统不可能将所有程序的输出信息一起显示,要知道后台执行的程序非常之多,如果一起显示,那我们不用操作了,整天只看 ...

  8. linux初级之总结复习

    一.linux命令复习 1.ls:列出当前目录下的文件 -h: -l: -d: -a: 2. man: 命令帮助手册 3. cd: 切换目录 -:  ~: ..: cd: 4. pwd: 显示当前工作 ...

  9. Java 自定义常量

    Java 中的常量就是初始化或赋值后不能再修改,而变量则可以重新赋值. 我们可以使用Java 关键字 final 定义一个常量,如下 final double PI = 3.14; 注意:为了区别 J ...

  10. MyBatis 高级查询环境准备(八)

    MyBatis 高级查询 之前在学习 Mapper XML 映射文件时,说到 resultMap 标记是 MyBatis 中最重要最强大也是最复杂的标记,而且还提到后面会详细介绍它的高级用法. 听到高 ...