BZOJ2096 [Poi2010]Pilots
来源:互联网 发布:穿帮镜头软件 编辑:程序博客网 时间:2024/05/29 14:23
题意:长度为n(1 <= n <= 3000000)的序列,要求从中选出最长的连续子序列,使得子序列中最大值-最小值 <= k
分析:
看到诸如最大值,最小值之类的东西,一个常见做法是使用单调栈/单调队列。
搞一个单增队列维护最小值,一个单减队列维护最大值。
枚举子序列的右端点,插入之后,两个队头乱搞就行了。
#include <cstdio>#include <algorithm>using namespace std;const int N = 3000005;int n,k,l1,r1,l2,r2,ans,t=1,a[N],q1[N],q2[N];int main() {scanf("%d%d", &k, &n);for(int i = 1; i <= n; i++) scanf("%d", &a[i]);for(int i = 1; i <= n; i++) {while(l1 < r1 && a[i] <= a[q1[r1-1]]) r1--;while(l2 < r2 && a[i] >= a[q2[r2-1]]) r2--;q1[r1++] = q2[r2++] = i;while(a[q2[l2]]-a[q1[l1]] > k) if(q2[l2]>q1[l1]) t=q1[l1]+1,l1++; else t=q2[l2]+1,l2++;ans = max(ans, i-t+1);}printf("%d", ans);return 0;}需要注意的一点是t要初始化,因为t表示的是上一个插入的左端点的位置,初始成0显然就走远了...
1 0
- bzoj2096【poi2010】Pilots
- 【POI2010】【BZOJ2096】Pilots
- BZOJ2096 [Poi2010]Pilots
- 【POI2010】BZOJ2096 pilots
- bzoj2096 [Poi2010]Pilots
- 【bzoj2096】[Poi2010]Pilots
- BZOJ2096 [Poi2010]Pilots
- 【bzoj2096】[Poi2010]Pilots 单调队列
- [BZOJ2096][Poi2010]Pilots(单调队列)
- [bzoj2096][Poi2010]Pilots(二分+单调队列)
- bzoj2096 pilots
- BZOJ2096 Pilots
- BZOJ2096: [Poi2010]Pilot
- [BZOJ2096][POI]Pilots
- [BZOJ 2096]Poi2010 Pilots
- BZOJ 2096: [Poi2010]Pilots
- BZOJ 2096 [Poi2010]Pilots
- [BZOJ 2096][Poi2010]Pilots
- 223. Rectangle Area
- 虚拟化技术比较 PV HVM
- 88. Merge Sorted Array
- GitLab的简单使用第一步
- JS设计模式之适配器模式
- BZOJ2096 [Poi2010]Pilots
- ubuntu12.04 crossover 安装方法
- 过多if-else分支的优化
- 要学习的东西6.11
- iOS给工程中选中的文件创建分组文件夹(小技巧)
- 剑指offer(16)-顺时针打印矩阵
- IPC机制
- Treap模板(中级版)
- 121~123188 Best Time to Buy and Sell StockI II III IV