求助80,RE两个点

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

十里剑神 @ 2022-10-08 22:13:12

#include<bits/stdc++.h>
using namespace std;
long long n,k,ans;
long long p_mmin,p_mmax;
long long a[9000005];
queue<long long> q;
set<long long> s;
vector<long long> mmax;
vector<long long> mmin;
int main(){
    cin>>n>>k;

    for(long long i=1;i<=n;i++){
        cin>>a[i];
    }
    for(long long i=1;i<=k;i++){
        s.insert(a[i]);
        q.push(a[i]);
    }
    auto find=s.end();
    find--;
    mmax.push_back(*find);
    auto find1=s.begin();
    find--;
    mmin.push_back(*find1);

    for(long long i=k+1;i<=n;i++){
        //cout<<q.back()<<" -->"<<q.front()<<endl;

        long long last=q.front();
        q.pop();
        q.push(a[i]);

        auto tempfind =s.find(last);
        s.erase(tempfind);
        //cout<<s.size()<<endl;
        s.insert(a[i]);
        auto find=s.end();
        find--;
        mmax.push_back(*find);
        auto find1=s.begin();
        find--;
        mmin.push_back(*find1);
    }

    for(long long i=0;i<mmin.size();i++){
        cout<<mmin[i]<<" ";
    }
    cout<<endl;
    for(long long i=0;i<mmax.size();i++){
        cout<<mmax[i]<<" ";
    }
    return 0;
}

|