poj 2823 Sliding Window 单调队列

Posted lpl_bys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2823 Sliding Window 单调队列相关的知识,希望对你有一定的参考价值。

Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 63635   Accepted: 18150
Case 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 positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

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 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source

-------------------------------------------------------------------------------------------------------------------
单调队列,顾名思义就是其中的元素单调递增或单调递减的队列。
在本题中,单调队列用来求在一段长度为n的数列中每段长度为k的子数列中的最大值和最小值。
去维护一段长为k的单调队列
对于新加入的数,要令其在队列中满足单调递增或递减;之后再排出在单调队列范围以外的head,此时队首元素即为数列中一段子数列的最大或最小值。具体看代码:
 
技术分享
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define maxn 1000010
 4 struct node{
 5     int pos,val;
 6 };
 7 node maxque[maxn];
 8 node minque[maxn];
 9 int read();
10 void getmax();
11 void getmin();
12 int n,m,a[maxn],ma[maxn],mi[maxn],op;
13 int main(){
14     n=read();m=read();
15     for(int i=1;i<=n;i++) a[i]=read();
16     getmax();
17     getmin();
18     for(int i=1;i<=op;i++) printf("%d ",mi[i]);
19     putchar(\n);
20     for(int i=1;i<=op;i++) printf("%d ",ma[i]);
21     return 0;
22 }
23 void getmax(){
24     int i,head=1,tail=0;
25     op=0;
26     for(i=1;i<m;i++){
27         while(head<=tail&&maxque[tail].val<a[i]) tail--;
28         maxque[++tail].pos=i;maxque[tail].val=a[i];
29     }
30     for(;i<=n;i++){
31         while(head<=tail&&maxque[tail].val<a[i]) tail--;
32         maxque[++tail].pos=i;maxque[tail].val=a[i];
33         while(maxque[head].pos<i+1-m) head++;
34         ma[++op]=maxque[head].val;
35     }
36 }
37 void getmin(){
38     int i,head=1,tail=0;
39     op=0;
40     for(i=1;i<m;i++){
41         while(head<=tail&&minque[tail].val>a[i]) tail--;
42         minque[++tail].pos=i;minque[tail].val=a[i];
43     }
44     for(;i<=n;i++){
45         while(head<=tail&&minque[tail].val>a[i]) tail--;
46         minque[++tail].pos=i;minque[tail].val=a[i];
47         while(minque[head].pos<i+1-m) head++;
48         mi[++op]=minque[head].val;
49     }
50 }
51 int read(){
52     int ans=0,f=1;char c=getchar();
53     while(0>c||c>9){if(c==-)f=-1;c=getchar();}
54     while(0<=c&&c<=9)ans=ans*10+c-48,c=getchar();return ans*f;
55 }
单调队列

 

以上是关于poj 2823 Sliding Window 单调队列的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2823 Sliding Window

POJ 2823 Sliding Window

POJ2823 Sliding Window

poj 2823 Sliding Window 题解

POJ#2823. Sliding Window

poj 2823 Sliding Window