Sliding Window(单调队列)

来源:互联网 发布:我知女人心评价 编辑:程序博客网 时间:2024/05/19 08:26
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 -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

也就是有一个数列a,要求你求数列b和c,b[i]是a[i]…a[i+w-1]中的最小值,c[i]是最大值。如果a是1,3,-1,-3,5,3,6,7,则b为-1,-3,-3,-3,3,3,c为3,3,5,5,6,7。

这个问题相当于一个数据流(数列a)在不断地到来,而数据是不断过期的,相当于我们只能保存有限的数据(sliding window中的数据,此题中就是窗口的宽度w),对于到来的查询(此题中查询是每时刻都有的),我们要返回当前滑动窗口中的最大值\最小值。注意,元素是不断过期的。

解决这个问题可以使用一种叫做单调队列的数据结构,它维护这样一种队列:

a)从队头到队尾,元素在我们所关注的指标下是递减的(严格递减,而不是非递增),比如查询如果每次问的是窗口内的最小值,那么队列中元素从左至右就应该递增,如果每次问的是窗口内的最大值,则应该递减,依此类推。这是为了保证每次查询只需要取队头元素。

b)从队头到队尾,元素对应的时刻(此题中是该元素在数列a中的下标)是递增的,但不要求连续,这是为了保证最左面的元素总是最先过期,且每当有新元素来临的时候一定是插入队尾。

满足以上两点的队列就是单调队列,首先,只有第一个元素的序列一定是单调队列。

那么怎么维护这个单调队列呢?无非是处理插入和查询两个操作。

对于插入,由于性质b,因此来的新元素插入到队列的最后就能维持b)继续成立。但是为了维护a)的成立,即元素在我们关注的指标下递减,从队尾插入新元素的时候可能要删除队尾的一些元素,具体说来就是,找到第一个大于(在所关注指标下)新元素的元素,删除其后所有元素,并将新元素插于其后。因为所有被删除的元素都比新元素要小,而且比新元素要旧,因此在以后的任何查询中都不可能成为答案,所以可以放心删除。

对于查询,由于性质b,因此所有该时刻过期的元素一定都集中在队头,因此利用查询的时机删除队头所有过期的元素,在不含过期元素后,队头得元素就是查询的答案(性质a),将其返回即可。

由于每个元素都进队出队一次,因此摊销复杂度为O(n)。

#include <iostream>#include <stdio.h>using namespace std;#define MAX 1000001int A[MAX]; //存储数据int Q[MAX]; //队列int P[MAX]; //存储A[i]中的下标iint Min[MAX];  //输出最小int Max[MAX];  //输出最大int n,k;void get_min(){    int i;    int head=1,tail=0;    for(i=0; i<k-1; i++)  //先把前两个入队    {        while(head<=tail && Q[tail]>=A[i])  //队尾元素大于要插入的数            --tail;        Q[++tail]=A[i];        P[tail]=i;    }    for(; i<n; i++)    {        while(head<=tail && Q[tail]>=A[i])            --tail;        Q[++tail]=A[i];        P[tail]=i;        while(P[head]<i-k+1)  //判断数是否过时,即窗口是否已经划过这个数,我这是从0开始计数的。        {            head++;        }        Min[i-k+1]=Q[head];    }}void get_max(){    int i;    int head=1,tail=0;    for(i=0; i<k-1; i++)    {        while(head<=tail && Q[tail]<=A[i])   //队尾元素小于要插入的值            --tail;        Q[++tail]=A[i];        P[tail]=i;    }    for(; i<n; i++)    {        while(head<=tail && Q[tail]<=A[i])   //队尾元素小于要插入的值            --tail;        Q[++tail]=A[i];        P[tail]=i;        while(P[head]<i-k+1)            {            head++;        }        Max[i-k+1]=Q[head];    }}void output(){       int i;      //输出最下值       for(i=0; i<n-k+1; i++)       {           if(i==0)               printf("%d",Min[i]);           else               printf(" %d",Min[i]);       }       printf("\n");       //输出最大值       for(i=0; i<n-k+1; i++)       {           if(i==0)               printf("%d",Max[i]);           else               printf(" %d",Max[i]);       }       printf("\n");}int main(){    int i;    //freopen("acm.txt","r",stdin);    scanf("%d%d",&n,&k);    for(i=0; i<n; i++)    {        scanf("%d",&A[i]);    }    get_min();    get_max();    output();    return 0;}


0 0
原创粉丝点击