POJ 2823 滑动窗口 单调队列模板

我们从最简单的问题开始:

给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k.

要求:

      f(i) = max{a(i-k+1),a(i-k+2),..., a(i)},i = 0,1,...,N-1

问题的另一种描述就是用一个长度为k的窗在整数数列上移动,求窗里面所包含的数的最大值。

解法一:

很直观的一种解法,那就是从数列的开头,将窗放上去,然后找到这最开始的k个数的最大值,然后窗最后移一个单元,继续找到k个数中的最大值。

这种方法每求一个f(i),都要进行k-1次的比较,复杂度为O(N*k)。

那么有没有更快一点的算法呢?

解法二:

我们知道,上一种算法有一个地方是重复比较了,就是在找当前的f(i)的时候,i的前面k-1个数其它在算f(i-1)的时候我们就比较过了。那么我们能不能保存上一次的结果呢?当然主要是i的前k-1个数中的最大值了。答案是可以,这就要用到单调递减队列。

单调递减队列是这么一个队列,它的头元素一直是队列当中的最大值,而且队列中的值是按照递减的顺序排列的。我们可以从队列的末尾插入一个元素,可以从队列的两端删除元素。

1.首先看插入元素:为了保证队列的递减性,我们在插入元素v的时候,要将队尾的元素和v比较,如果队尾的元素不大于v,则删除队尾的元素,然后继续将新的队尾的元素与v比较,直到队尾的元素大于v,这个时候我们才将v插入到队尾。

2.队尾的删除刚刚已经说了,那么队首的元素什么时候删除呢?由于我们只需要保存i的前k-1个元素中的最大值,所以当队首的元素的索引或下标小于i-k+1的时候,就说明队首的元素对于求f(i)已经没有意义了,因为它已经不在窗里面了。所以当index[队首元素]<i-k+1时,将队首元素删除。

(补充:队列中的元素主要包括了两个性质:大小--决定它是否影响f[i]的求值,时效性(删除队头使用)--数组下标决定它是否已经离开了滑动窗口,不在影响f[i]了,删除队尾时,是新入队的时效性>已在队中的元素了,如果队尾的取值不如新入队的元素的值优的话,那么就可以删除队尾了。)

从上面的介绍当中,我们知道,单调队列与队列唯一的不同就在于它不仅要保存元素的值,而且要保存元素的索引(当然在实际应用中我们可以只需要保存索引,而通过索引间接找到当前索引的值)。

为了让读者更明白一点,我举个简单的例子。

假设数列为:8,7,12,5,16,9,17,2,4,6.N=10,k=3.

那么我们构造一个长度为3的单调递减队列:

首先,那8和它的索引0放入队列中,我们用(8,0)表示,每一步插入元素时队列中的元素如下:

0:插入8,队列为:(8,0)

1:插入7,队列为:(8,0),(7,1)

2:插入12,队列为:(12,2)

3:插入5,队列为:(12,2),(5,3)

4:插入16,队列为:(16,4)

5:插入9,队列为:(16,4),(9,5)

。。。。依此类推

那么f(i)就是第i步时队列当中的首元素:8,8,12,12,16,16,。。。

例题:POJ 2823 滑动窗口

                                              Sliding Window

Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 54158   Accepted: 15543
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


转载自https://www.cnblogs.com/c1299401227/p/5774133.html;
#include<iostream>
#include<stdio.h>
#include<cstring>
#define N 1000050
#define LL long long
using namespace std;
LL q[N];//记录的是单调队列中元素在a数组中的下标
int n,k,a[N],I[N];
void getmax()//求窗口内最大值,单调递减队列,首地址所在的元素就是最大值
{
    int head=1,tail=0;//head和tail分别是队列头指针和尾指针
    for(int i=1;i<k;i++)//现在1到k-1的优先队列先计算出来
    {
        while(head<=tail&&a[q[tail]]<=a[i]) 
            tail--;
        
        q[++tail]=i;
    }
    for(int i=k;i<=n;i++)//计算k到n的优先队列
    {
        while(head<=tail&&a[q[tail]]<=a[i])
            tail--;
        q[++tail]=i; 
        
        while(q[head]<=i-k)
            head++; //如果这个数不在当前区间内那么先不取这个数,再向当前区间选
        printf("%d ",a[q[head]]); 
    } 
} 
void getmin()//求窗口内最小值,单调递增队列,首地址所在的元素就是最小值
{ 
    int head=1,tail=0;
    for(int i=1;i<k;i++)//现在1到k-1的优先队列先计算出来
    {
        while(head<=tail&&a[q[tail]]>=a[i])
         tail--;
        
        q[++tail]=i;
    }
    for(int i=k;i<=n;i++)
    {
        while(head<=tail&&a[q[tail]]>=a[i])
            tail--;
        q[++tail]=i; 
        
        while(q[head]<=i-k)
            head++; //如果这个数不在当前区间内那么先不取这个数,再向当前区间选
        printf("%d ",a[q[head]]); 
    } 
} 

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&k)!=EOF&&n)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        getmin();
        printf("
");
        getmax();
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/-citywall123/p/11257961.html