台州 OJ 3847 Mowing the Lawn 线性DP 单调队列
描述
After winning the annual town competition for best lawn a year ago, Farmer John has grown lazy; he has not mowed the lawn since then and thus his lawn has become unruly. However, the competition is once again coming soon, and FJ would like to get his lawn into tiptop shape so that he can claim the title.
Unfortunately, FJ has realized that his lawn is so unkempt that he will need to get some of his N (1 <= N <= 100,000) cows, who are lined up in a row and conveniently numbered 1..N, to help him. Some cows are more efficient than others at mowing the lawn; cow i has efficiency E_i (0 <= E_i <= 1,000,000,000).
FJ has noticed that cows near each other in line often know each other well; he has also discovered that if he chooses more than K (1 <= K <= N) consecutive (adjacent) cows to help him, they will ignore the lawn and start a party instead. Thus, FJ needs you to
assist him: determine the largest total cow efficiency FJ can obtain without choosing more than K consecutive cows.
输入
* Line 1: Two space-separated integers: N and K
* Lines 2..N+1: Line i+1 contains the single integer: E_i
输出
* Line 1: A single integer that is the best total efficiency FJ can obtain.
#include <iostream>
#include <cstring>
#define LL __int64
using namespace std;const int MAX = 100005;LL dp[MAX]; //不放 i 时,前 i 个数字组成的序列,去掉的数字的最小和
LL que[MAX]; //单调队列
int pos[MAX]; //队列中元素对应的下标
int front, rear;
int n, m;int main(){// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);scanf("%d%d", &n, &m);LL num;LL sum = 0;LL ans;memset(dp, 0x3f, sizeof(dp));dp[0] = 0; //去掉第一个 front = 0; rear = 1; //初始化单调队列 que[rear] = 0;pos[rear] = 0;for(int i=1; i<=n; i++){if(rear != front && pos[front+1] < i-m-1){ //如果前面的数字过期了 front++;}scanf("%I64d", &num);sum += num;//计算 dp[i]dp[i] = que[front+1] + num;//将 dp[i] 放入队列while(rear != front && que[rear] > dp[i]){rear--;} que[++rear] = dp[i];pos[rear] = i; if(i == n-m){ans = dp[i];}if(i >= n-m){ans = min(ans, dp[i]);}}//最后 m+1 个数字中,在哪个地方断开 printf("%I64d\n", sum-ans);return 0;
}
转载于:https://www.cnblogs.com/lighter-blog/p/7397162.html
台州 OJ 3847 Mowing the Lawn 线性DP 单调队列相关推荐
- 烽火传递(dp+单调队列)
烽火台又称烽燧,是重要的军事防御设施,一般建在险要或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情,在某两座城市之间有 n 个烽火台,每个烽火台发出信号都有一 ...
- URAL 1427. SMS(DP+单调队列)
题目链接 我用的比较传统的办法...单调队列优化了一下,写的有点搓,不管怎样过了...两个单调队列,存两个东西,预处理一个标记数组存... 1 #include <iostream> 2 ...
- POJ 3017 DP + 单调队列 + 堆
题意:给你一个长度为n的数列,你需要把这个数列分成几段,每段的和不超过m,问各段的最大值之和的最小值是多少? 思路:dp方程如下:设dp[i]为把前i个数分成合法的若干段最大值的最小值是多少.dp转移 ...
- [DP/单调队列]BZOJ 2059 [Usaco2010 Nov]Buying Feed 购买饲料
首先我想吐槽的是题目并没有表明数据范围... 这个题目 DP方程并不难表示. dp[i][j]表示前i个地点携带了j个货物的最小花费 dp[i][j] = dp[i-1][k] + (j-k) * c ...
- [luogu 4292][bzoj 1758][WC2010] 重建计划(点分治 + dp + 单调队列优化 + 启发式合并)
[WC2010]重建计划 problem solution code problem 洛谷指路 solution 一看那个道路平均价值的式子:AvgValue=∑e∈Sv(e)∣S∣\text{Avg ...
- bzoj1791,P4381-[IOI2008]Island【基环树,树形dp,单调队列dp,树的直径】
正题 评测记录:https://www.luogu.org/recordnew/lists?uid=52918&pid=P4381 题目大意 有n个岛,n条无向边(保证每个岛都有边连到).走过 ...
- bzoj2500幸福的道路 树形dp+单调队列
2500: 幸福的道路 Time Limit: 20 Sec Memory Limit: 256 MB Submit: 434 Solved: 170 [Submit][Status][Discu ...
- Codeforces Round #521 (Div. 3): F. Pictures with Kittens(DP+单调队列)
题意: 你有n幅画,第i幅画的好看程度为ai,再给你两个数字k,x,表示你要从中选出刚好x幅画,并且相邻两幅画的距离不能≥k,好看程度之和最大能多少,选不出来输出-1,F1数据范围<200,F2 ...
- Codeforces 1077F2 Pictures with Kittens (hard version)(DP+单调队列优化)
题目链接:Pictures with Kittens (hard version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:数据量5000, ...
- bzoj5185 [Usaco2018 Jan]Lifeguards(dp+单调队列优化)
真是太神了orz 我们先贪心地把被包含的线段删掉,把剩下的线段按左端点排序,这样的话右端点显然也是有序的. 设dp[i][k],表示前i个线段,删了k个,且必须保留i线段的最大覆盖长度.枚举上一个线段 ...
最新文章
- Vue 教程第十七 篇—— Vuex 之 module
- 简单三步搭建一对一直播源码系统
- 操作系统知识点_内存管理
- 评测 | 千元以下的扫拖一体机器人,到底值不值得买?
- 剑指offer没有java版吗_剑指Offer(Java版) 持续更新中
- 【HTML5 4】《HTML5与CSS3权威指南》 step1 导读
- [2019杭电多校第六场][hdu6635]Nonsense Time
- 后摩尔时代下先进封装技术
- 全国第三次土壤普查实验室筛选开始 实验室要求理化检测指标仪器一览
- Mongodb模式设计
- Linux命令:halt
- 解决onenote 错误 0xE000078D
- not executable: 64-bit ELF file 已解决
- opencv分离RGB三通道
- 短视频抖音账号矩阵seo优化系统技术代开发
- css border实现图形
- esp32 flash加密介绍
- linux中查看文件大小
- macOS实现词典正常查询维基百科
- QT安装教程(简易)