POJ-2823 Sliding Window

这是我的第一发题解,纪念一下233

题面如下:

An array of size n ≤ 10 6 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
题意:给你一个长度为N的数组,一个长为K滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:
[ 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


分析:我们依然从艾(bao)神(li)的解题思路入手。对于每一次滑动,我们可以枚举每一个小窗口的所有值,找到最小与最大的值,复杂度也很显然有O(n*k),直接就被我们否决了。

本题当然可以开一个线段树,在O(nlogn) 的时间复杂度内就可以完成所有的操作,但是这显然不够优秀,并且在这儿开一个线段树实在是大材小用了。P.S. 这儿不能使用树状数组。(为什么?我也不知道QAQ)

另外一个O(nlogn) 的方法就是我昨天的博客里面提到的方法,把问题转化为RMQ问题,ST表预处理,并对每个k区间求最值。这儿有个问题,开二维数组的话会MLE。至于解决方法,实际上因为每次查询的都是定长区间k,所以我们开一个一维数组就好,在这儿也不多阐述。

接下来就是今天用到的标解了,我们按照惯例先来分析一手。以最小值为例,对于样例中的序列而言,我们在框到[1 3 -1]的时候答案是-1,然后滑到下一个位置,即[3 -1 -3]的时候为-3,此时我们可以预见到,至少是在往后滑2个单位的时候,-3之前的3和-1都是不可能成为答案的,于是我们可以愉快地把它们去掉,变成[-3]。因此,我们可以看见在框的范围内,它的答案是一个递增的序列。于是我们就可以构造一个递增的单调队列,每次往里加元素的时候,只要维护好这个单调队列的性质,就可以得到每次的队头元素就是我们要寻找的区间最小值。

在构造单调队列的时候,要注意以下几个问题。每次往队尾插元素的时候,为了保证队列的单调性,必须把队尾所有大于待插元素的元素全都弹出去直到队空或者队尾元素小于待插元素。此时判断队中元素个数是不是大于窗口k,如果是的话,就弹出队首元素直到不大于k位置,接着在队尾插入待插元素即可。

上面这部分代码就是MLE的ST表代码,下面就是单调队列,这儿使用数组模拟队列(因为deque的话好像也会T?)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>

using namespace std;

//const int maxn=1e6+7;
//int stmax[maxn][20],stmin[maxn][20];
//int a[maxn];
//
//void rmq_st(int n){
//    for(int i=1;i<=n;i++)
//        stmax[i][0]=stmin[i][0]=a[i];
//    int m=(int)(double(log(n))/log(2.0));
//    for(int j=1;j<=m;j++)
//    for(int i=1;i+(1<<j)-1<=n;i++){
//        stmax[i][j]=max(stmax[i][j-1],stmax[i+(1<<j-1)][j-1]);
//        stmin[i][j]=min(stmin[i][j-1],stmin[i+(1<<j-1)][j-1]);
//    }
//}
//
//int rmq_maxquery(int l,int r){
//    int k=(int)(double(log(r-l+1))/log(2.0));
//    return max(stmax[l][k],stmax[r-(1<<k)+1][k]);
//}
//int rmq_minquery(int l,int r){
//    int k=(int)(double(log(r-l+1))/log(2.0));
//    return min(stmin[l][k],stmin[r-(1<<k)+1][k]);
//}
//
//
//int main(){
//    int n,k;
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    while(cin>>n>>k){
//        for(int i=1;i<=n;i++)
//            cin>>a[i];
//        rmq_st(n);
//        k--;
//        for(int i=1;i+k<n;i++)
//            cout<<rmq_minquery(i,i+k)<<" ";
//        cout<<rmq_minquery(n-k,n)<<endl;
//        for(int i=1;i+k<n;i++)
//            cout<<rmq_maxquery(i,i+k)<<" ";
//        cout<<rmq_maxquery(n-k,n)<<endl;
//
//
//    }
//
//    return 0;
//}

const int maxn=1e6+7;
int a[maxn];
int maxq[maxn];
int minq[maxn];
int q[maxn];
int n,k;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>n>>k){
        for(int i=1;i<=n;i++)
            cin>>a[i];
        int head,tail,t;
        memset(q,0,sizeof(q));
        head=1,tail=1;
        q[tail]=1;
        minq[1]=a[1];
        for(int i=2;i<=n;i++){
            while(head<=tail&&a[i]<a[q[tail]])
                tail--;
            q[++tail]=i;
            if(head<=tail&&q[head]<i-k+1)
                head++;
            minq[i]=a[q[head]];
        }
        memset(q,0,sizeof(q));
        head=1,tail=1;
        q[tail]=1;
        maxq[1]=a[1];
        for(int i=2;i<=n;i++){
            while(head<=tail&&a[i]>a[q[tail]])
                tail--;
            q[++tail]=i;
            if(head<=tail&&q[head]<i-k+1)
                head++;
            maxq[i]=a[q[head]];
        }
        for(int i=k;i<n;i++)
            cout<<minq[i]<<" ";
        cout<<minq[n]<<endl;
        for(int i=k;i<n;i++)
            cout<<maxq[i]<<" ";
        cout<<maxq[n]<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值