90???

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

sf2166751838 @ 2020-12-23 20:29:59

#include<iostream>
#include<cstdio>
using namespace std;
int a[1000006],q[1000006];
int main()
{
    int n,k,h=1,t=1;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    q[h]=1,q[t]=1;
    for(int i=2;i<=n;i++)
    {
        while( h<=t && a[q[t]]>=a[i]) t--;
        q[++t]=i;
        if(i-q[h]>=k) h++;
        if(i>=k) 
        cout<<a[q[h]]<<" ";
    }
    cout<<endl;
    t=1,h=1,q[h]=1,q[t]=1;
    for(int i=2;i<=n;i++)
    {
        while( h<=t && a[q[t]]<=a[i]) t--;
        q[++t]=i;
        if(i-q[h]>=k) h++;
        if(i>=k) 
        cout<<a[q[h]]<<" ";
    }
    return 0;
}

by BADFIVE @ 2020-12-23 20:31:42

主页双贴?


by _短笛无腔_ @ 2020-12-23 20:31:55

双贴危

虽然正经


by 天命之路 @ 2020-12-23 20:45:01

@sf2166751838 你要特判k=1


|