POJ 2823 Sliding Window (单调队列)

题目链接

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 knumbers 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

PS:单调队列的入门题,首先我们声明两个数组(ma_que[maxn],mi_que[maxn])保存当前区间的最小值的下标,和最大值的下标,然后声明四个变量,表示两个数组的头指针位置和尾指针位置(不知道为什么G++超时,C++就过了)。

首先要理解一下,两个数组的头结点保存的是当前区间最小(最大)值的下标,看下面这个代码,每次从队尾开始查找,一直找到当前能够替换的数据,要是都比当前数大(小)就会一直跳到头结点位置,然后当前最优解就会替换掉头结点保存的节点值。要是一个比当前数大(小)的数都没有,当前数的坐标直接插入到尾指针位置,要是有一些比当前数大(小)的数,就会替换头指针后尾指针中间的位置。(替换是替换掉比当前更优的值)。

            while(mahead<=matail&&num[ma_que[matail]]<=num[i])//删除队尾元素
                matail--;
            ma_que[++matail]=i;
            while(mihead<=mitail&&num[mi_que[mitail]]>=num[i])
                mitail--;
            mi_que[++mitail]=i;

 因为每次都要保证是在当前k长度的区间中找到最小值,最大值,所以我们要保证头指针保存的位置一定在当前的区间中。

            while(mahead<=matail&&ma_que[mahead]<=i-k)//删除下标超出范围的元素
                mahead++;
            while(mihead<=mitail&&mi_que[mihead]<=i-k)
                mihead++;

结合下来;

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e6+10;
const int mod=100000000;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
int n,k;
int ma_ans[maxn],mi_ans[maxn];
int num[maxn],ma_que[maxn],mi_que[maxn];
int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        int mahead=1,mihead=1,matail=0,mitail=0;
        for(int i=1;i<=n;i++)
        { 
            scanf("%d",&num[i]);
            while(mahead<=matail&&ma_que[mahead]<=i-k)//删除下标超出范围的元素
                mahead++;
            while(mihead<=mitail&&mi_que[mihead]<=i-k)
                mihead++;
            while(mahead<=matail&&num[ma_que[matail]]<=num[i])//删除队尾元素
                matail--;
            ma_que[++matail]=i;
            while(mihead<=mitail&&num[mi_que[mitail]]>=num[i])
                mitail--;
            mi_que[++mitail]=i;
            ma_ans[i]=num[ma_que[mahead]];
            mi_ans[i]=num[mi_que[mihead]];
        }
        for(int i=k;i<=n;i++)
            printf("%d ",mi_ans[i]);
        printf("\n");
        for(int i=k;i<=n;i++)
            printf("%d ",ma_ans[i]);
        printf("\n");
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值