70分求助,怎么优化

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

nbtngnllmd @ 2022-07-13 14:26:31

#include<cstdio>
int a[1000001],x[1000001],y[1000001] ;int front=1,tail,zhi1=1,zhi2=1;
int main(){
    int n,k;scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    tail=k;
    while(tail-1!=n)
    {
        int max=1000001,min=-1000001;
        for(int i = front; i <= tail;i++)
        {
            if(a[i]>min) min=a[i];
            if(a[i]<max) max=a[i];  
        }
        x[zhi1]=min;y[zhi2]=max;
        zhi1++;zhi2++;
        tail++;
        front++;
    }
    for(int i=1;i<=zhi2-1;i++) printf("%d ",y[i]);
    printf("\n");
    for(int i=1;i<=zhi1-1;i++) printf("%d ",x[i]);
    return 0;
}

by Kalpas_s2006 @ 2022-07-13 14:49:04

复杂度不对,换个算法,建议运用deque,如果不会可以学习理解题解


by nbtngnllmd @ 2022-07-13 15:38:35

谢谢大佬


|