90pts

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

lcy0506 @ 2024-03-23 19:48:34

#include <bits/stdc++.h>
using namespace std;
long long a[1000001],minn[1000001],maxx[1000001];
long long p[1000001],q[1000001];
int main()
{
    int n,m,h=0,t=1;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        if(h<=t&&p[h]<i-m+1) 
        h++;
        while(h<=t&&q[t]>=a[i]) 
        t--;
        t++;
        q[t]=a[i];
        p[t]=i;
        minn[i]=q[h];   
    }
    h=1,t=0;
    for(int i=1;i<=n;i++)
    {
        if(h<=t&&p[h]<i-m+1) 
        h++;
        while(h<=t&&q[t]<=a[i]) 
        t--;
        t++;
        q[t]=a[i];
        p[t]=i;
        maxx[i]=q[h];   
    }
    for(int i=m;i<=n;i++)
    cout<<minn[i]<<" ";
    cout<<endl;
    for(int i=m;i<=n;i++)
    cout<<maxx[i]<<" ";
}

90pts记录


|