单调队列全部RE求助

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

Yikara @ 2021-10-05 11:26:52

#include <bits/stdc++.h>
#define pass
#define GrandTheftAutoV int
using namespace std;
const int maxn=1e6+1;
int n,k;
struct s{
    int d,v;
}a[maxn];
deque <s>q;
void up(){
    for(GrandTheftAutoV i=1;i<=n;i++){
        while(!q.empty()&&a[i].v>=q.front().v){
            q.pop_front();
        }
        q.push_front(a[i]);
        while(q.back().d<=i-k)q.pop_back();
        if(i>=k)cout<<q.back().v<<" ";
    }
    puts("");
}
void down(){
    for(GrandTheftAutoV i=1;i<=n;i++){
        while(!q.empty()&&a[i].v<=q.front().v){
            q.pop_front();
        }
        q.push_front(a[i]);
        while(q.back().d>i-k)q.pop_back();
        if(i>=k)cout<<q.back().v<<" ";
    }
}
void clear(){
    while(!q.empty())q.pop_front();
}
void read(){
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for(GrandTheftAutoV i=1;i<=n;i++){
        cin>>a[i].v;
        a[i].d=i;
    }
}
int main(){
    read();
    up();
    clear();
    down();
    return 0;
}

by myEnd @ 2021-10-05 11:34:39

快去扶老奶奶过马路


by Yikara @ 2021-10-05 11:34:55

@int256 ?????


by 三重门123456 @ 2021-11-17 15:37:03

ios::sync_with_stdio(false);

本语句意思为关闭于stdio的同步。所以用了这条就不要用stdio的了


|