首页 > 代码库 > poj 2823 单调队列

poj 2823 单调队列

Sliding Window
Time Limit: 12000MS
Memory Limit: 65536K
Total Submissions: 59348
Accepted: 17040
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 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

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

POJ Monthly--2006.04.28, Ikki

 

 //单调队列

 #include<algorithm>
#include<cstdio>
using namespace std;
int a[1000011];//数组数据
int Q[1000011];//队列
int I[1000011];//I[i]表示队列中的Q[i]在数组中的下标
int OutMin[1000011];//最小值
int OutMax[1000011];//最大值
int n,k;
void GetMin()
{
    int i;
    int head=1;
    int tail=0;
    for(i=1;i<k;++i)
    {
        while(head <=tail && Q[tail]>a[i])
            tail--;
        tail++;
        Q[tail]=a[i];
        I[tail]=i;
    }
    for(i=k;i<=n;++i)
    {
        while(head <=tail && Q[tail]>a[i])
            tail--;
        tail++;
        Q[tail]=a[i];
        I[tail]=i;
        while(I[head]<=i-k)
            head++;
        OutMin[i-k]    = Q[head];
    }

}
void GetMax()
{
    int i;
    int head=1;
    int tail=0;
    for(i=1;i<k;++i)
    {
        while(head <=tail && Q[tail]<a[i])
            tail--;
        tail++;
        Q[tail]=a[i];
        I[tail]=i;
    }
    for(i=k;i<=n;++i)
    {

        while(head <=tail && Q[tail]<a[i])
            tail--;
        tail++;
        Q[tail]=a[i];
        I[tail]=i;
        while(I[head]<=i-k)
            head++;
        OutMax[i-k]=Q[head];
    }

}
int main()
{
    int i;
    scanf("%d%d",&n,&k);
    if(k>n)
        k=n;
    for(i=1;i<=n;++i)
    {
        scanf("%d",&a[i]);
    }
    GetMin();
    GetMax();
    for(i=0;i<(n-k);++i)
    {
        printf("%d ", OutMin[i]);

    }
    printf("%d\n", OutMin[n-k]);
    for(i=0;i<=(n-k);++i)
    {
        printf("%d ", OutMax[i]);
    }
    printf("%d\n", OutMax[i]);
    
    return 0;
}

 

 

 

//优先队列

#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int a[1000011];//数组数据

int OutMin[1000011];//最小值

int OutMax[1000011];//最大值

int cnt1=0;
int cnt2=0;
int n,k;

struct cmp1
{
    bool operator()(const int a1,const int a2)
    {
        return a[a1]>a[a2];
    }
};
struct cmp2
{
    bool operator()(const int a1,const int a2)
    {
        return a[a1]<a[a2];
    }
};
priority_queue <int ,vector<int>,cmp1> Q1;
priority_queue <int ,vector<int>,cmp2> Q2;
int main()
{
    int i;
    scanf("%d%d",&n,&k);
    if(k>n)
        k=n;
    for(i=1;i<=n;++i)
    {
        scanf("%d",&a[i]);
    }
    for(i=1;i<=k;++i)
    {
        Q1.push(i);
        Q2.push(i);
    }
    OutMin[cnt1++]=a[Q1.top()];
    OutMax[cnt2++]=a[Q2.top()];
    for(i=k+1;i<=n;++i)
    {
        Q1.push(i);
        Q2.push(i);
        while(i-Q1.top()>=k)
            Q1.pop();
        OutMin[cnt1++]=a[Q1.top()];
        while(i-Q2.top()>=k)
            Q2.pop();
        OutMax[cnt2++]=a[Q2.top()];
    }
    
    for(i=0;i<=(n-k);++i)
    {
        printf("%d%c", OutMin[i], (i < n - k) ? ‘ ‘ : ‘\n‘);
        
    }
    for(i=0;i<=(n-k);++i)
    {
        printf("%d%c", OutMax[i], (i < n - k) ? ‘ ‘ : ‘\n‘);
    }
    return 0;
}

poj 2823 单调队列