POJ 2823 Sliding Window

题意  给你n个数,然后求连续m个数的最小值和最大值

然后维护一个单调递增序列和一个单调递减序列

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int last,head;
int n,m,k;
int a[1111111];

int que1[1111111];
int que2[1111111];
int ans1[1111111];
int ans2[1111111];

int main(){
    scanf("%d%d",&n,&m);{
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        int l1=0;
        int l2 = 0;
        int ans = 0;
        int l = 0;
        int r1 = -1;
        int r2 = -1;
        int cunt =0;
        for(int i=1;i<=n;i++){
            while(l1<=r1&&a[que1[r1]]>a[i]) --r1;
            while(l2<=r2&&a[que2[r2]]<a[i]) --r2;
            que1[++r1] = i;
            que2[++r2] = i;
            if(i>=m){
            while(que1[l1]<=i-m){l1++;}
            while(que2[l2]<=i-m){l2++;}
            ans1[++cunt] = a[que1[l1]];
            ans2[cunt] = a[que2[l2]];
            }
        }
        for(int i=1;i<cunt;i++){
            printf("%d ",ans1[i]);
        }
        printf("%d\n",ans1[cunt]);
        for(int i=1;i<cunt;i++){
            printf("%d ",ans2[i]);
        }
        printf("%d\n",ans2[cunt]);

    }
}


你可能感兴趣的:(POJ 2823 Sliding Window)