再次求助,请各位帮帮我

P1886 滑动窗口 /【模板】单调队列

NiveusNix @ 2019-08-29 19:44:35

90分,re第三个点

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
int a[1000000+10],n,m;
deque <int> q;
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=m;i++){
        if(q.empty()){
            q.push_back(i);
            continue;
        }
        while(a[i]<a[q.back()]){
            q.pop_back();
            if(q.empty()){
                break;
            }
        }
        q.push_back(i);
    }
    cout<<a[q.front()]<<" ";
    for(int i=m+1;i<=n;i++){
        if(q.empty()){
            q.push_back(i);
            continue;
        }
        if(q.front()<=i-m){
            q.pop_front();
        }
        while(a[i]<a[q.back()]){
            q.pop_back();
            if(q.empty()){
                break;
            }
        }
        q.push_back(i);
        cout<<a[q.front()]<<" ";
    }
    cout<<endl;
    q.clear();
    for(int i=1;i<=m;i++){
        if(q.empty()){
            q.push_back(i);
            continue;
        }
        while(a[i]>a[q.back()]){
            q.pop_back();
            if(q.empty()){
                break;
            }
        }
        q.push_back(i);
    }
    cout<<a[q.front()]<<" ";
    for(int i=m+1;i<=n;i++){
        if(q.empty()){
            q.push_back(i);
            continue;
        }
        if(q.front()<=i-m){
            q.pop_front();
        }
        while(a[i]>a[q.back()]){
            q.pop_back();
            if(q.empty()){
                break;
            }
        }
        q.push_back(i);
        cout<<a[q.front()]<<" ";
    }
    return 0;
}

by G_A_TS @ 2019-08-29 21:56:33

@kkk


by G_A_TS @ 2019-08-29 21:56:46

@ltt


上一页 |