bzoj 2096: [Poi2010]Pilots (二分答案+单调队列)
来源:互联网 发布:mpv播放器 mac下载 编辑:程序博客网 时间:2024/05/29 03:35
2096: [Poi2010]Pilots
Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 719 Solved: 368
[Submit][Status][Discuss]
Description
Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串,任意两个难度差不会超过他设定的最大值。耍畸形一个人是不行的,于是他找到了你。
Input
输入:第一行两个有空格隔开的整数k(0<=k<=2000,000,000),n(1<=n<=3000,000),k代表Tz设定的最大值,n代表难度序列的长度。第二行为n个由空格隔开的整数ai(1<=ai<=2000,000,000),表示难度序列。
Output
输出:最大的字串长度。
Sample Input
3 9
5 1 3 5 8 6 6 9 10
5 1 3 5 8 6 6 9 10
Sample Output
4
(有两个子串的长度为4: 5, 8, 6, 6 和8, 6, 6, 9.最长子串的长度就是4)
(有两个子串的长度为4: 5, 8, 6, 6 和8, 6, 6, 9.最长子串的长度就是4)
HINT
Source
by poi
题解:二分答案+单调队列
二分一个区间长度,用单调队列求出该长度的所有区间的最大值和最小值,然后判断是否在k之内.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 3000003using namespace std;int a[N],q[N],head,tail,n,k,maxn[N],minn[N];bool pd(int x){head=tail=1; q[head]=1;for (int j=2;j<=n;j++) {while (q[head]<j-x+1&&head<=tail) head++;while (a[q[tail]]<=a[j]&&head<=tail) tail--;q[++tail]=j;if (j-x+1>0) maxn[j-x+1]=a[q[head]];}tail=head=1; q[head]=1;for (int j=2;j<=n;j++){while (q[head]<j-x+1&&head<=tail) head++;while (a[q[tail]]>=a[j]&&head<=tail) tail--;q[++tail]=j;if (j-x+1>0) minn[j-x+1]=a[q[head]];}for (int i=1;i<=n-x+1;i++) if (maxn[i]-minn[i]<=k) return true;return false;}int main(){scanf("%d%d",&k,&n);for (int i=1;i<=n;i++) scanf("%d",&a[i]);int l=1; int r=n; int ans=0;while (l<=r) {int mid=(l+r)/2;if (pd(mid)) ans=max(ans,mid),l=mid+1;else r=mid-1;}printf("%d\n",ans);}
0 0
- bzoj 2096: [Poi2010]Pilots (二分答案+单调队列)
- BZOJ 2096([Poi2010]Pilots-单调队列-差值)
- BZOJ 2096 Poi2010 Pilots 单调队列
- BZOJ 2096: [Poi2010]Pilots|单调队列
- bzoj 2096: [Poi2010]Pilots 单调队列
- [BZOJ 2096][Poi2010]Pilots:单调队列
- 单调队列 BZOJ 2096 [Poi2010]Pilots
- BZOJ 2096: [Poi2010]Pilots 单调队列题解
- BZOJ 2096 [Poi2010]Pilots 单调队列
- [bzoj2096][Poi2010]Pilots(二分+单调队列)
- 【BZOJ】【P2096】【Poi2010】【Pilots】【题解】【二分+单调队列】
- [BZOJ2096][Poi2010]Pilots(单调队列)
- 【bzoj2096】[Poi2010]Pilots 单调队列
- BZOJ 2096: pilots 单调队列
- [BZOJ 2096]Poi2010 Pilots
- BZOJ 2096: [Poi2010]Pilots
- BZOJ 2096 [Poi2010]Pilots
- [BZOJ 2096][Poi2010]Pilots
- hive使用beeline测试JDBC连接
- Android——Hook(钩子函数)动态注入代码
- Jedis
- linux命令学习-文件目录之rm命令
- 关于constructor 属性查看对象是否为数组详情
- bzoj 2096: [Poi2010]Pilots (二分答案+单调队列)
- 第一次用Python写的冒泡
- 用OPEN CV SVM 的例子程序
- C++调用C库一直提示link错误解决办法
- 自定义的alert
- mysql中的常用查询持续更新中
- 14.7.2 Changing the Number or Size of InnoDB Redo Log Files 改变InnoDB Redo Log Files的数量和大小
- linux命令学习-文件目录之mv命令
- php基础之一---流程控制