poj 2823 Sliding Window 单调队列
来源:互联网 发布:php curl 请求头 编辑:程序博客网 时间:2024/06/05 18:05
Language:
Sliding Window
Time Limit: 12000MS Memory Limit: 65536KTotal Submissions: 36485 Accepted: 10805Case Time Limit: 5000MS
Description
An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k 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.Window position Minimum value Maximum value [1 3 -1] -3 5 3 6 7 -13 1 [3 -1 -3] 5 3 6 7 -33 1 3 [-1 -3 5] 3 6 7 -35 1 3 -1 [-3 5 3] 6 7 -35 1 3 -1 -3 [5 3 6] 7 36 1 3 -1 -3 5 [3 6 7]37
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.
Input
The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.
Output
There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.
Sample Input
8 31 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 33 3 5 5 6 7
Source
POJ Monthly--2006.04.28, Ikki
题意:求每一个长度为k的区间的最大值,最小值,把它们都输出来。
思路:开两个单调队列分别维护非单调增和非单调减。
详见代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=1000000+100;int n,k;int head1,tail1,head2,tail2;int ans[2][MAXN];struct node{ int val,index;}que1[MAXN],que2[MAXN];int main(){ //freopen("text.txt","r",stdin); while(~scanf("%d%d",&n,&k)) { head1=tail1=head2=tail2=0; for(int i=1;i<=n;i++) { int x; scanf("%d",&x); while(head1<tail1 && que1[tail1-1].val>x) tail1--; que1[tail1].val=x; que1[tail1++].index=i; while(head2<tail2 && que2[tail2-1].val<x) tail2--; que2[tail2].val=x; que2[tail2++].index=i; if(que1[head1].index<i-k+1 && head1<tail1) head1++; if(que2[head2].index<i-k+1 && head2<tail2) head2++; ans[0][i]=que1[head1].val; ans[1][i]=que2[head2].val; } printf("%d",ans[0][k]); for(int i=k+1;i<=n;i++) printf(" %d",ans[0][i]); printf("\n"); printf("%d",ans[1][k]); for(int i=k+1;i<=n;i++) printf(" %d",ans[1][i]); printf("\n"); } 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 (单调队列)
- ZOJ 3635 树状数组+二分
- 虚拟机VirtualBox里redhat跟主机windows 7安装增强包和设置共享文件
- 面向对象编程,我的思想(上部)
- 面向对象编程,我的思想(下部)
- 使用componentsSeparatedByCharactersInSet:方法限定输入字符的格式
- poj 2823 Sliding Window 单调队列
- myeclipse10配置maven
- Android菜鸟在成长-ListView1
- DetectSourceDemo
- uva 439(bfs)
- 【SQL】Oracle的内连接、左外连接、右外连接及全外连接
- 理解Linux系统/etc/init.d目录和/etc/rc.local脚本
- 自定义事件
- [生活]SAE初次体验