POJ2823 Sliding Window (单调队列)

模板:熟练到5分钟之内打出

POJ2823 Sliding Window (单调队列)
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e6+7;
const int inf=0x3f3f3f3f;
int f[N];
int q[N];
int a[N];
int main(){
    int i;
    int n,k;
    cin>>n>>k;
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    int hh=0;
    int tt=-1;
    for(i=1;i<=n;i++){
        while(hh<=tt&&q[hh]<=i-k)
            hh++;
        while(hh<=tt&&a[q[tt]]>=a[i])
            tt--;
        q[++tt]=i;
        if(i>=k)
            cout<<a[q[hh]]<<" ";
    }
    cout<<endl;
    hh=0;
    tt=-1;
    for(i=1;i<=n;i++){
        while(hh<=tt&&q[hh]<=i-k)
            hh++;
        while(hh<=tt&&a[q[tt]]<=a[i])
            tt--;
        q[++tt]=i;
        if(i>=k)
            cout<<a[q[hh]]<<" ";
    }
    cout<<endl;
    return 0;

}
View Code

 

POJ2823 Sliding Window (单调队列)

上一篇:如何用redis统计海量UV?


下一篇:c# 判断类是否实现了某个接口的三种方法