bzoj2442 修剪草坪——单调队列
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2442
设 f[i] 为答案,则有 f[i] = max { f[j] - s[j+1] } + s[i] ;
所以用单调队列维护 f[i] - s[i+1];
没有1A!!原因是遍历的起点不是1而是0,因为还可以不选第一个;残念。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int const maxn=1e5+;
int n,k,head,tail;
ll f[maxn],s[maxn];
struct N{ll w;int pos;}q[maxn];
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%lld",&s[i]);s[i]+=s[i-];
}
head=;tail=;
for(int i=;i<=n;i++)
{
while(head<=tail&&q[head].pos<i-k-)head++;
if(i>k)f[i]=q[head].w+s[i];
else f[i]=s[i];
while(head<=tail&&q[tail].w<=f[i]-s[i+])tail--;
q[++tail].w=f[i]-s[i+];q[tail].pos=i;
}
printf("%lld",f[n]);
return ;
}
bzoj2442 修剪草坪——单调队列的更多相关文章
- bzoj2442[Usaco2011 Open]修剪草坪 单调队列优化dp
2442: [Usaco2011 Open]修剪草坪 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1159 Solved: 593[Submit] ...
- BZOJ 2442: [Usaco2011 Open]修剪草坪 单调队列
Code: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- P2627 修剪草坪 (单调队列优化$dp$)
题目链接 Solution 70分很简单的DP,复杂度 O(NK). 方程如下: \[f[i][1]=max(f[j][0]+sum[i]-sum[j])\]\[f[i][0]=max(f[i-1][ ...
- 修剪草坪 单调队列优化dp BZOJ2442
题目描述 在一年前赢得了小镇的最佳草坪比赛后,Farm John变得很懒,再也没有修剪过草坪.现在,新一轮的最佳草坪比赛又开始了,Farm John希望能够再次夺冠. 然而,Farm John的草坪非 ...
- bzoj2442[Usaco2011 Open]修剪草坪——单调队列优化
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2442 考虑记录前 i 个.末尾 j 个连续选上的最大值.发现时空会爆. 又发现大量的转移形如 ...
- [BZOJ2442][Usaco2011 Open]修剪草坪 dp+单调队列优化
2442: [Usaco2011 Open]修剪草坪 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1118 Solved: 569[Submit] ...
- BZOJ2442 Usaco2011 Open修剪草坪(动态规划+单调队列)
显然可以dp.显然可以单调队列优化一下. #include<iostream> #include<cstdio> #include<cmath> #include& ...
- 【BZOJ2442】修建草坪(动态规划,单调队列)
[BZOJ2442]修建草坪(动态规划,单调队列) 题面 权限题..洛谷 题解 设\(f[i]\)表示前\(i\)个里面选出来的最大值 转移应该比较显然 枚举一个断点的位置,转移一下就好 \(f[i] ...
- BZOJ_2343_[Usaco2011 Open]修剪草坪 _单调队列_DP
BZOJ_2343_[Usaco2011 Open]修剪草坪 _单调队列_DP 题意: N头牛,每头牛有一个权值,选择一些牛,要求连续的不能超过k个,求选择牛的权值和最大值 分析: 先考虑暴力DP,f ...
随机推荐
- [codeforces538E]Demiurges Play Again
[codeforces538E]Demiurges Play Again 试题描述 Demiurges Shambambukli and Mazukta love to watch the games ...
- 简单的Fleury算法模板
假设数据输入时采用如下的格式进行输入:首先输入顶点个数n和边数m,然后输入每条边,每条边的数据占一行,格式为:u,v,表示从顶点u到顶点v的一条有向边 这里把欧拉回路的路径输出了出来: 手写栈: #i ...
- [luoguP2073] 送花(set)
传送门 set #include <set> #include <cstdio> #include <iostream> #define LL long long ...
- PHP应用日期与时间
<?php/* 时间戳 * * 1. 是一个整数 * 2. 1970-1-1 到现在的秒数 1213212121 * * 2014-02-14 11:11:11 * * 02/14/2014 1 ...
- 调用BOS服务保存一个单据的简化示例
IMetaDataService metadataService = ServiceHelper.GetService<IMetaDataService>(); // 加载元数据 Form ...
- HDU 4651 (生成函数)
HDU 4651 Partition Problem : n的整数划分方案数.(n <= 100008) Solution : 参考资料: 五角数 欧拉函数 五边形数定理 整数划分 一份详细的题 ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]
传送门 C. Om Nom and Candies time limit per test 1 second memory limit per test 256 megabytes input sta ...
- ***iOS学习之Table View的简单使用和DEMO示例(共Plain普通+Grouped分组两种)
Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...
- 动态规划: HDU 1789Doing Homework again
Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...
- topcoder 650 srm
500 遇到这种构造题 就给跪了 比赛的时候想很多方法 DP,贪心,模拟 发现越写越烦琐.看到别人出这么快,肯定又是奇葩思路. 后来居然想到 2^50的暴力 +剪枝 不过暴力肯定卡你 IDEA: 只要 ...