#11WA 求解

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

shihan @ 2024-11-06 01:37:46

#include<bits/stdc++.h>
using namespace std;
long long a[1000005];
vector<long long>maxi;
vector<long long>mini;
int main()
{
    int n,k;
    deque<int>q;
    cin>>n>>k;
    for (int i=0;i<n;i++)
        cin>>a[i];
    for (int i=0;i<n;i++)
    {
        while (!q.empty() && a[q.back()]>=a[i])
            q.pop_back();
        q.push_back(i);
        if (i-q.front()>=k) q.pop_front();
        if (i>=k-1) cout<<a[q.front()]<<" ";
    }   
    cout<<endl;

    for (int i=0;i<n;i++)
    {
        while (!q.empty() && a[q.back()]<=a[i])
            q.pop_back();
        q.push_back(i);
        if (i-q.front()>=k) q.pop_front();
        if (i>=k-1) cout<<a[q.front()]<<" ";
    }   
    cout<<endl;
    return 0;
}

by komorebi17 @ 2024-12-01 21:34:01

中间q.clear()一下


|