2823 Sliding Window 求区间最值 单调队列

时间:2021-03-13 17:39:13
Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 14477   Accepted: 4075
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 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 31 3 -1 -3 5 3 6 7

Sample Output

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

 

 

 

 

 

开两个队列,一个维护最大值,一个维护最小值。为了方便叙述,下文只讨论最大队,
最小队的维护方式类似。我们要保证队列中各个元素大小单调递减(注意,不是单调不上升)
,各个元素的下标单调递增。这样才可以保证队首元素最大,而且更新的时候队首永远
是当前最大。因此,需要改造一下队列,让它变成能在两头删除,在队尾插入。为了保
证单调性,每次插入的时候,先判断队尾元素,如果不比待插入元素大就删除,不断删除
队尾直到队尾元素大于待插入元素或者队空。删除的时候,判断队首,如果队首元素下标小
于当前段左边界就删除,不断删除队首直到队首元素下标大于等于当前段左边界(注意:这
时队列肯定不为空),队首元素就是当前段的最优解。有了单调队列,计算max[i]和min[i]
就方便了。初始的时候分别把当前行前n-1 个元素插入队尾。从第1列开始,每次取队首最优
值,从队首删除无效元素,并将当前列+n-1 号元素插入队尾。由于每个元素最多入队出队各
一次,时间复杂度就是O(b)。
#include<iostream>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=1100000;
int Q[inf];//单调队列
int I[inf];//I[i]表示队列中的Q[i]在数组中的下标

int _min[inf],_max[inf];//存放最值
int a[inf];//数组数据
int n,k;//数组长度n,求连续k个数的最值
void GetMax()//最大队
{
    int head=1,tail=0;//队列头和尾[head,tail];
    for(int i=1;i<k;i++)
    {
        while(head<=tail&&Q[tail]<=a[i]) tail--;//a[i]入队
        tail++;
        Q[tail]=a[i],I[tail]=i;
    }
    for(int i=k;i<=n;i++)
    {
        while(head<=tail&&Q[tail]<=a[i]) tail--;//a[i]入队
        tail++;
        Q[tail]=a[i],I[tail]=i;
        while(I[head]<=i-k) head++;//a[i-k]出队
        _max[i]=Q[head];//保存
    }
}
void GetMin()//最小队
{
    int head=1,tail=0;//队列头和尾[head,tail];
    for(int i=1;i<k;i++)
    {
        while(head<=tail&&Q[tail]>=a[i]) tail--;//a[i]入队
        tail++;
        Q[tail]=a[i],I[tail]=i;
    }
    for(int i=k;i<=n;i++)
    {
        while(head<=tail&&Q[tail]>=a[i]) tail--;//a[i]入队
        tail++;
        Q[tail]=a[i],I[tail]=i;
        while(I[head]<=i-k) head++;//a[i-k]出队
        _min[i]=Q[head];//保存
    }
}
int main()
{
    while(scanf("%d%d",&n,&k)==2)
    {
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        GetMax();
        GetMin();
        for(int i=k;i<=n;i++)
        {
            printf("%d%c", _min[i], (i < n ) ? ' ' : '/n');
        }
        for(int i=k;i<=n;i++)
        {
            printf("%d%c", _max[i], (i < n ) ? ' ' : '/n');
        }
    }
    return 0;
}