[洛谷P3512 [POI2010]PIL-Pilots]
题目链接:###
题目分析:###
感觉不是很难啊……不像是蓝题(AC量也不像)恶意评分?
少打了一个+1调了半天,就这样居然还能过60pts?我思路和题解第一篇高度重合是什么鬼啊,太过分了吧本来还想水篇题解的
单调队列分别维护序列最大值和最小值,如果极差\(>k\)的话就扔掉最大值和最小值里面靠前的那一个,因为你不能不改动这个连续子序列的左端点而从中间丢掉一个值。同时记录下这个序列的左端点\(last_l\)是扔掉的那个值的编号+1(只要丢掉这个值,剩下的序列就合法了),丢完之后更新\(ans=max(ans,i-last_l+1)\)即可。时间复杂度为\(O(n)\)。
其实是一道不错的单调队列题。
题做少了看什么题都感觉眉清目秀的
代码:###
#include<bits/stdc++.h>
#define N (3000000+5)
using namespace std;
inline int read(){
int cnt=0,f=1;char c;
c=getchar();
while(!isdigit(c)){
if(c=='-')f=-f;
c=getchar();
}
while(isdigit(c)){
cnt=cnt*10+c-'0';
c=getchar();
}
return cnt*f;
}
int n,k,l1=1,r1=0,x,ans,l2=1,r2=0;
int last_l=1;
struct node{
int dat;int pos;
} q1[N],q2[N];
int main(){
k=read();n=read();
if(n==1) {
printf("1");
return 0;
}
for(register int i=1;i<=n;i++){
x=read();
while(l1<=r1&&q1[r1].dat>x)r1--;
q1[++r1].dat=x;q1[r1].pos=i;
while(l2<=r2&&q2[r2].dat<x)r2--;
q2[++r2].dat=x;q2[r2].pos=i;
while(q2[l2].dat-q1[l1].dat>k){
if(q1[l1].pos<q2[l2].pos&&l1<=r1){
last_l=q1[l1].pos+1;
l1++;
}
if(q1[l1].pos>=q2[l2].pos&&l2<=r2){
last_l=q2[l2].pos+1;
l2++;
}
}
if(i-last_l+1>ans)ans=i-last_l+1;
}
printf("%d",ans);
return 0;
}
[洛谷P3512 [POI2010]PIL-Pilots]的更多相关文章
- 洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速$dp\&Floyd$)
洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速\(dp\&Floyd\)) 标签:题解 阅读体验:https://zybuluo.com/Junl ...
- [洛谷P3501] [POI2010]ANT-Antisymmetry
洛谷题目链接:[POI2010]ANT-Antisymmetry 题目描述 Byteasar studies certain strings of zeroes and ones. Let be su ...
- 洛谷 P3496 [POI2010]GIL-Guilds
P3496 [POI2010]GIL-Guilds 题目描述 King Byteasar faces a serious matter. Two competing trade organisatio ...
- 洛谷 P3507 [POI2010]GRA-The Minima Game
P3507 [POI2010]GRA-The Minima Game 题目描述 Alice and Bob learned the minima game, which they like very ...
- 洛谷 P3505 [POI2010]TEL-Teleportation
P3505 [POI2010]TEL-Teleportation 题目描述 King Byteasar is the ruler of the whole solar system that cont ...
- 【字符串】【hash】【倍增】洛谷 P3502 [POI2010]CHO-Hamsters 题解
这是一道字符串建模+图论的问题. 题目描述 Byteasar breeds hamsters. Each hamster has a unique name, consisting of lo ...
- 洛谷P3507 [POI2010]GRA-The Minima Game
题目描述 Alice and Bob learned the minima game, which they like very much, recently. The rules of the ga ...
- [洛谷P3509][POI2010]ZAB-Frog
题目大意:有$n$个点,每个点有一个距离(从小到大给出),从第$i$个点跳一次,会跳到距离第$i$个点第$k$远的点上(若有两个点都是第$k$远,就跳到编号小的上).问对于从每个点开始跳,跳$m$次, ...
- 洛谷 P3375 【模板】KMP字符串匹配
我这段时间因为字符串太差而被关了起来了(昨晚打cf不会处理字符串现场找大佬模板瞎搞,差点就凉了),所以决定好好补一下字符串的知识QAQ,暂时先学习kmp算法吧~ 题目链接:https://www.lu ...
随机推荐
- cocos2d-x(vs2012)环境搭建(第一篇)[版本号:cocos2d-x-3.1.1]
1.下载资源 下载cocos2d-x包V3.1.1,下载戳这里: http://www.cocos2d-x.org/download vs2012下载戳这里: http://www.xiazaiba. ...
- string和int互相转化
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
- CocoaPods初体验
之前没用过cocoapods,但是新项目需要用到. 安装cocoapods: 按照官方的: $ sudo gem install cocoapods // 但是什么都没有发生 升级gem $ s ...
- java.lang.IllegalStateException: No instances available for localhost
在SpringCloud的项目中,我们使用了自动配置的OAuth2RestTemplate,RestTemplate,但是在使用这些restTemplate的时候,url必须是服务的名称,如果要调用真 ...
- antV G6流程图在Vue中的使用
最近我司项目中需要加入流程图制作功能,于是乎百度各种找可视化绘制拓扑图的轮子,大部分都是国外的,看文档太吃力,不过好在最终让我发现了AntV G6流程图图表库,最新版为2.0,不过编辑器在2.0版本还 ...
- POJ3685 Matrix —— 二分
题目链接:http://poj.org/problem?id=3685 Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissio ...
- 使用JavaScript访问XML数据
在本篇文章中,我们将讲述如何在IE中使用ActiveX功能来访问并解析XML文档,由此允许网络冲浪者操纵它们.这一网页将传入并运行脚本的初始化.你一定确保order.xml文档与jsxml.html在 ...
- 跨平台实现zip压缩加密功能
使用zlib将文件夹压缩成zip文件时,需要自己读取文件然后写入zip文件.利用官方下载的zlib包中包含的contrib/minizip/zip.h和zip.c代码提供的函数,可以很容易实现这个功能 ...
- exception.py
try: raise Exception except Exception as e: print(e) try: raise Exception('comment') except Exceptio ...
- [SHOI 2013] 发微博
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4419 [算法] 用std :: set维护每个人的好友集合 当两人成为好友时将每人接 ...