P3594 [POI2015]WIL-Wilcze doły
P3594 [POI2015]WIL-Wilcze doły
题目描述
给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0。请找到最长的一段连续区间,使得该区间内所有数字之和不超过p。
输入格式:
第一行包含三个整数n,p,d(1<=d<=n<=2000000,0<=p<=10^16)。第二行包含n个正整数,依次表示序列中每个数wi。
调试日志: 开始 p 和 d 输反了。。。查了半天、、老想自己哪里想错了QAQ
Solution
首先感性认识一下, 随着某一特定的右端点的增加, 其对应的最优左端点是会增加的
也就是说左端点随着右端点的单调递增
贪心认识一下, 由于元素大小大于0, 消去的能选满一定选满
所以处理出 \(maxx[i]\) 表示以 \(i\) 作为起点, (在不越界的情况下)选满的值
因为左端点随右端点单调递增
我们枚举每一个右端点, 同时单调队列处理出现区间内的最大能消去值 \(M\)
若是仍 \(sum[i] - sum[now - 1] - M > p\) 那么只好更新左端点了
每次更新 \(ans\) 即可
Code
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
#define REP(i, x, y) for(LL i = (x);i <= (y);i++)
using namespace std;
LL RD(){
LL out = 0,flag = 1;char c = getchar();
while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
return flag * out;
}
const LL maxn = 4000019;
LL num, d, p;
LL a[maxn];
LL maxx[maxn];//以i开头的满长消去值
LL sum[maxn];
void init(){
num = RD(), p = RD(), d = RD();
REP(i, 1, num)a[i] = RD(), sum[i] = sum[i - 1] + a[i];
REP(i, 1, num){
LL now = i + d - 1 > num ? sum[num] : sum[i + d - 1];
maxx[i] = now - sum[i - 1];
}
}
struct Que{
LL val, Index;
}Q[maxn];//存maxx
LL head = 1, tail = 0;
void push_back(LL v, LL i){
while(head <= tail && Q[tail].val <= v)tail--;
Q[++tail] = (Que){v, i};
}
LL now;//目前真实队列的队头
LL get_max(){
while(head <= tail && Q[head].Index < now)head++;
return Q[head].val;
}
LL ans;
void solve(){
now = 1;
REP(i, d, num){
push_back(maxx[i - d + 1], i - d + 1);
LL M = get_max();
while(sum[i] - sum[now - 1] - M > p)now++, M = get_max();
ans = max(ans, i - now + 1);
}
printf("%lld\n", ans);
}
int main(){
init();
solve();
return 0;
}
CG

P3594 [POI2015]WIL-Wilcze doły的更多相关文章
- BZOJ 4385: [POI2015]Wilcze doły
4385: [POI2015]Wilcze doły Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 648 Solved: 263[Submit][ ...
- [POI2015]Wilcze doły
[POI2015]Wilcze doły 题目大意: 给定一个长度为\(n(n\le2\times10^6)\)的数列\(A(1\le A_i\le10^9)\),可以从中选取不超过\(d\)个连续数 ...
- 【BZOJ4385】[POI2015]Wilcze doły 单调栈+双指针法
[BZOJ4385][POI2015]Wilcze doły Description 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段 ...
- BZOJ4385 : [POI2015]Wilcze doły
求出前缀和$s$,设$f[i]=s[i+d-1]-s[i-1]$. 从左到右枚举的右端点$i$,左端点$j$满足单调性,若$s[i]-s[j-1]-\max(区间内最大的f)\leq p$,则可行. ...
- BZOJ4385[POI2015]Wilcze doły——单调队列+双指针
题目描述 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. 输入 第一行包含三个整数n,p ...
- bzoj 4385: [POI2015]Wilcze doły【单调栈】
对于每个i,以它为左端点的最优右端点一定是单增的,所以用单调栈维护 具体的,单调栈里放的是和单调的长为d的子段,然后枚举右端点,如果这段的和-当前长为d子段最大的和大于p的话,左端点右移同时注意单调栈 ...
- 【bzoj4385】[POI2015]Wilcze doły
单调队列扫描,记录当前区间长度为d的一段的和的最大值,和当前区间和. #include<algorithm> #include<iostream> #include<cs ...
- bzoj4385 Wilcze doły
Description 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. Input 第一 ...
- bzoj4385 & POJ2015 Wilcze doły
Description 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. Input 第一 ...
随机推荐
- 《Linux内核设计与实现》读书笔记 1&2
第一章 Linux内核简介 1.2追寻Linus足迹:linux简介 Linus开发.Linux是类Unix系统.Linux内核也是自由软件. 1.3操作系统和内核简介 操作系统:在整个系统中负 ...
- 数学建模-lingo使用
1.安装启动,软件下载地址:http://pc.xzstatic.com/2017/06/LINGO14 .zip.此为免安装版,打开后双击Lingo11.exe即可启动软件. 2.示例:某商品单位成 ...
- OSG 改变窗口大小
viewer.realize();//需要realize,否则窗口为null osgViewer::GraphicsWindow *pWnd = dynamic_cast<osgViewer:: ...
- Maven项目中添加JDBC驱动
在pom.xml配置文件中添加: <dependency> <groupId>mysql</groupId> <artifactId>mysql-con ...
- FileUtils功能概述
https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.ht ...
- What Is Apache Hadoop
What Is Apache Hadoop? The Apache™ Hadoop® project develops open-source software for reliable, scala ...
- back to top 回到顶部按钮 css+js
效果 html <p id="back-to-top"><a href="#top"><span></span> ...
- php函数值传值/地址以及引用的用法
博客摘自 奔跑的大白,网址: http://www.cnblogs.com/gauze/p/5568867.html 1.先来解释一下名词. 值传递(passl-by-value)过程中,被调函数的 ...
- scipy插值interpolation
>>> from scipy.interpolate import interp1d#interp1d表示1维插值 >>> >>> x = np. ...
- poj3320(尺取法)
题目大意:给你一串数字,找出最小的能够覆盖所有出现过的数字的区间长度: 解题思路:依旧是尺取法,但要用map标记下出现过的书: 代码:别用cin输入: #include<iostream> ...