码迷,mamicode.com
首页 > Windows程序 > 详细

poj 2823 Sliding Window (单调队列)

时间:2015-07-29 22:44:43      阅读:714      评论:0      收藏:0      [点我收藏+]

标签:

Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 46705   Accepted: 13485
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

 
 
关于单调队列的讲解:

看这个问题: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.Your task is to determine the maximum and minimum values in the sliding window at each position.

也就是有一个数列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)。

POJ2823就是上面描述的那道题。

http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html  

讲得很好.我认为重点的地方加颜色了.

如果要找最大值,那么比新加入的元素小且旧的元素是不可能成为答案的,可以放心删去!

理解了这句话就理解了单调队列...

上面那个博客将得是挺好,只是代码写得略丑.

自己按照思想写了个还看得过去的

/*************************************************************************
    > File Name: code/2015summer/单调队列/B.cpp
    > Author: 111qqz
    > Email: rkz2013@126.com 
    > Created Time: 2015年07月29日 星期三 20时18分16秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)  
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=1E6+7;
int a[N],mxans[N],q[N],indx[N],mians[N];
int n,k;
int head,tail;

int main()
{
    cin>>n>>k;
    for ( int   i = 0 ; i < n ; i++ )
    {
    scanf("%d",&a[i]);
    }
    head = 1 ; tail = 0;
    for ( int i = 0; i < n ; i++)
    {
    if (indx[head]+k-1<i)
    {
        head++;
    }
    while (head<=tail&&q[tail]<=a[i]) tail--;
    tail++;
    q[tail]=a[i];
    indx[tail]=i;
    if (i>=k-1)
    {
        mxans[i-k+1]=q[head];
    }
    }
    head = 1 ; tail = 0;
    memset(q,0,sizeof(q));
    for ( int i  = 0 ; i < n ; i++)
    {
    if (indx[head]+k-1<i)
    {
        head++;
    }
    while (head<=tail&&q[tail]>=a[i]) tail--;
    tail++;
    q[tail]=a[i];
    indx[tail]=i;
    if (i>=k-1)
    {
        mians[i-k+1]=q[head];
    }
    }
    for ( int i = 0 ; i < n-k+1 ; i++ )
    {
    if (i==0)
        cout<<mians[i];
    else cout<<" "<<mians[i];
    }
    cout<<endl;
    for ( int i = 0 ; i < n-k+1 ; i++ )
    {
    if (i==0)
        cout<<mxans[i];
    else cout<<" "<<mxans[i];
    }
  
    return 0;
}

 

 

 

poj 2823 Sliding Window (单调队列)

标签:

原文地址:http://www.cnblogs.com/111qqz/p/4687327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!