POJ 2823 Sliding Window(单调队列)
来源:互联网 发布:淘宝如何下架宝贝 编辑:程序博客网 时间:2024/06/11 05:19
单调队列典型题
An array of size n ≤ 10 6 is given to you. There is a sliding window of sizek which is moving from the very left of the array to the very right. You can only see thek numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Your task is to determine the maximum and minimum values in the sliding window at each position.
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string>#include<map> #include<set>#define eps 1e-6 #define LL long long using namespace std; const int maxn = 2000000;//const int INF = 0x3f3f3f3f;int n, k;int a[maxn];//单调队列int qmin[maxn], vmin[maxn], hmin = 1, tmin = 0; void Min(int a, int i) { //第i个元素a入队 while(hmin<=tmin && vmin[hmin] <= i-k) hmin++; //超范围队首出队 //while(hmin<=tmin && qmin[tmin]>=a) tmin--; //不符合要求队尾出列 int l = hmin, r = tmin;while(l <= r) {int m = l+(r-l)/2;if(qmin[m] >= a) r = m - 1;else l = m + 1; }tmin = ++r;qmin[tmin] = a;vmin[tmin] = i;}int qmax[maxn], vmax[maxn], hmax = 1, tmax = 0; void Max(int a, int i) { //第i个元素a入队 while(hmax<=tmax && vmax[hmax] <= i-k) hmax++; //超范围队首出队 //while(hmax<=tmax && qmax[tmax]<=a) tmax--; //不符合要求队尾出列 int l = hmax, r = tmax;while(l <= r) {int m = l+(r-l)/2;if(qmax[m] <= a) r = m - 1;else l = m + 1; }tmax = ++r;qmax[tmax] = a;vmax[tmax] = i;}int ansMax[maxn], ansMin[maxn];int main() {//freopen("input.txt", "r", stdin);while(scanf("%d%d", &n, &k) == 2) {hmin = 1, tmin = 0;hmax = 1, tmax = 0;for(int i = 1; i <= n; i++) scanf("%d", &a[i]);for(int i = 1; i < k; i++) {Min(a[i], i);Max(a[i], i);}for(int i = k; i <= n; i++) {Min(a[i], i);ansMin[i-k] = qmin[hmin];Max(a[i], i);ansMax[i-k] = qmax[hmax];}for(int i = 0; i <= n-k; i++) if(i != n-k) printf("%d ", ansMin[i]);else printf("%d\n", ansMin[i]);for(int i = 0; i <= n-k; i++) if(i != n-k) printf("%d ", ansMax[i]);else printf("%d\n", ansMax[i]);}return 0;}
0 0
- poj 2823 Sliding Window (单调队列)
- POJ 2823 Sliding Window 堆 / 单调队列
- POJ 2823 Sliding Window 单调队列
- poj 2823 Sliding Window 【单调队列】
- poj 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- poj 2823 Sliding Window(简单单调队列)
- POJ 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- poj 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- poj 2823 Sliding Window(单调队列)
- POj 2823 Sliding Window 单调队列
- poj 2823 Sliding Window ( 单调队列 )
- 单调队列--poj-2823-Sliding Window
- POJ 2823 Sliding Window (单调队列)
- POJ:2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window (单调队列)
- JavaScript权威指南_141_第15章_脚本化文档_15.9-HTML表单-表单和元素的的事件处理程序
- android:ellipsize实现跑马灯效果总结
- Win7下MongoDB的安装和部署测试
- JavaSE初学笔记之<并发编程—内存模型、可见性、原子性>
- poj2010解题报告(堆)
- POJ 2823 Sliding Window(单调队列)
- Ajax初体验之登录校验
- Android 动画之Translate
- hdu 5313 Bipartite Graph 完全二分图 深搜 bitset应用
- JAVA笔记之函数
- SharePoint 2013 搭建负载均衡(NLB)
- Android 动画介绍汇总
- Evaluate Reverse Polish Notation
- Java之数据类型在内存中存储的方式