POJ 2823 Sliding Window(经典单调队列)

An array of size  n ≤ 10  6 is given to you. There is a sliding window of size  kwhich 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

 【题意】有一个数列n,显示宽度为k,数列从显示屏上滚过(没错,就是滚过),求每时刻显示屏上k个数中的最小值和最大值。

 【分析】 这题是经典的单调队列模拟题,在每次进来一个数时,和它的前一个数比较,比它大(小),就把前一个数弹出去,继续向前比较,直到遇到比它小(大)的数,就把它放在这个数后面,因为弹出去的数比后面的数小,所以任何时候屏幕上包含这个数时,这个数都不可能成为最大(最小)数,所以删掉也就无所谓了。所以这样执行下去每次队首的元素就是要求的那个最大(最小)值,输出即可。


 这里面是我自己手写的队列;

 用时5407ms;

 参考博客:http://blog.csdn.net/code_pang/article/details/14104487

 【代码】:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1e6+5;

int str[N];
int head;
int tail;

int a[N];
int n;
int k;

void push_min(int i)
{
    while(tail>head && a[i]<a[str[tail-1]])
        tail--;
    str[tail++]=i;
}

void push_max(int i)
{
    while(tail>head && a[i]>a[str[tail-1]])
        tail--;
    str[tail++]=i;
}

bool isempty(void)
{
    return head==tail;
}

void pop(void)
{
    head++;
}

int Front(void)
{
    return str[head];//注意了,不是直接返回Head
}

void Get_max()
{
    head=tail=0;

    int i;
    for(i=1;i<=k&& i<=n;i++)
    {
        push_max(i);
    }
    printf("%d",a[Front()]);

    for(;i<=n;i++)
    {
        while(!isempty() && Front()+k <=i)
        {
            pop();
        }
        push_max(i);
        printf(" %d",a[Front()]);
    }
    printf("\n");
}

void Get_min()
{
    head=tail=0;
    int i;
    for(i=1;i<=k && i<=n;i++)
        push_min(i);
    printf("%d",a[Front()]);

    for(;i<=n;i++)
    {
        while(!isempty() && Front()+k <=i)
            pop();
        push_min(i);
        printf(" %d",a[Front()]);
    }
    printf("\n");
}

int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        Get_min();
        Get_max();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值