Description

求一个最长的序列,最大值最小值之差不超过 \(k\) .

Sol

单调队列.

一个队列直接上就行..

Code

/**************************************************************
Problem: 2096
User: BeiYu
Language: C++
Result: Accepted
Time:4024 ms
Memory:71604 kb
****************************************************************/ #include <bits/stdc++.h>
using namespace std; #define debug(a) cout<<#a<<"="<<a<<" " typedef long long LL;
const int N = 3e6+50; LL n,k,ans;
LL a[N];
LL q[2][N],h[2],t[2],l; inline LL in(LL x=0,char ch=getchar()) { while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();return x; }
int main() {
k=in(),n=in();
for(int i=1;i<=n;i++) a[i]=in(); ans=1;
q[0][h[0]=t[0]=1]=1,q[1][h[1]=t[1]=1]=1,l=1; for(int i=2;i<=n;i++) {
while(h[0]<=t[0] && a[i]<a[q[0][t[0]]]) t[0]--;
q[0][++t[0]]=i; // cout<<i<<":"<<endl;
// for(int j=h[0];j<=t[0];j++) cout<<q[0][j]<<endl; while(h[1]<=t[1] && a[i]>a[q[1][t[1]]]) t[1]--;
q[1][++t[1]]=i; // for(int j=h[1];j<=t[1];j++) cout<<q[1][j]<<endl; for(;a[q[1][h[1]]]-a[q[0][h[0]]]>k;l++) {
if(q[0][h[0]]==l) h[0]++;
if(q[1][h[1]]==l) h[1]++;
}
ans=max(ans,i-l+1);
// debug(l),debug(ans)<<endl;
// cout<<"--------------------"<<endl;
}cout<<ans<<endl;
return 0;
}

BZOJ 2096: [Poi2010]Pilots的更多相关文章

  1. BZOJ 2096: [Poi2010]Pilots( set )

    用个set维护就可以水过去...O(NlogN) 应该可以用单调队列O(N).... --------------------------------------------------------- ...

  2. 【刷题】BZOJ 2096 [Poi2010]Pilots

    Description Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串,任意两个难度差不会超过他设定的最大值.耍畸形一个人是 ...

  3. BZOJ——2096: [Poi2010]Pilots

    http://www.lydsy.com/JudgeOnline/problem.php?id=2096 Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: ...

  4. BZOJ 2096: [Poi2010]Pilots 单调队列

    Code: #include<bits/stdc++.h> #define maxn 4000000 using namespace std; void setIO(string s) { ...

  5. BZOJ 2096([Poi2010]Pilots-单调队列-差值)

    2096: [Poi2010]Pilots Time Limit: 30 Sec   Memory Limit: 162 MB Submit: 190   Solved: 97 [ Submit][ ...

  6. bzoj2096[Poi2010]Pilots 单调队列

    2096: [Poi2010]Pilots Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 983  Solved: 513[Submit][Statu ...

  7. 【BZOJ2096】[Poi2010]Pilots 双指针+单调队列

    [BZOJ2096][Poi2010]Pilots Description Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串, ...

  8. BZOJ 2096 Pilots - 单调队列STL(deque)

    传送门 分析: 单调队列:维护两个递增.递减的队列,每次都加入新元素并更新,如果最大值(递减队首)-最小值(递增队首) > k,那么将最左段更新为前面两者中较前的那一个,并弹掉.用deque可以 ...

  9. BZOJ 2080: [Poi2010]Railway 双栈排序

    2080: [Poi2010]Railway Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 140  Solved: 35[Submit][Statu ...

随机推荐

  1. Gulp 入门

    1. 安装 Node 环境 参考 http://www.cnblogs.com/zichi/p/4627728.html,注意一起安装 npm 工具,并把路径保存到环境变量中(安装过程中会有提醒) 安 ...

  2. mybatis generator maven插件自动生成代码

    如果你正为无聊Dao代码的编写感到苦恼,如果你正为怕一个单词拼错导致Dao操作失败而感到苦恼,那么就可以考虑一些Mybatis generator这个差价,它会帮我们自动生成代码,类似于Hiberna ...

  3. git常用命令

    开始的时候 git config --global user.name "Your Name" git config --global user.email "email ...

  4. 由于服务器意外的断电,导致SQL SERVER服务器上数据库出现“置疑”而无法使用,

    来自百度 1.停止数据库服务器,将数据库MDF文件和LDF文件复制备份一份2.启动数据库服务器,删除置疑的数据库3.仅用备份的数据库MDF文件附加数据库,sp_attach_db或者sp_attach ...

  5. Git 耍不起来啊

    1.  $ git clone https://******.git Cloning into 'dt-engine-server'...fatal: unable to access 'https: ...

  6. Windows API Hooking in Python

    catalogue . 相关基础知识 . Deviare API Hook Overview . 使用ctypes调用Windows API . pydbg . winappdbg . dll inj ...

  7. Android Studio解决未识别Java文件(出现红J)问题

    1.问题:java文件出现了红J的问题,正常情况下应该是显示蓝色的C标识. 2.解决方案:切换到project视图下,找到app这个module里的build.gradle,在android结构里插入 ...

  8. AngularJs ng-repeat限制循环次数

    重复数组:<ul ng-init='name=[1,2,3,3]'> <li ng-repeat="name in name track by $index"&g ...

  9. Opera 浏览器各版本下载地址

    新版本下载地址: 正式分支: http://get.opera.com/ftp/pub/opera/desktop/ beta分支:http://get.opera.com/ftp/pub/opera ...

  10. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...