STL输出不对

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

Delayed @ 2024-09-19 17:36:00

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')w=-1;
        ch=getchar();
    }
    while(ch<='9' && ch>='0')
    {
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*w;
}
int n,k;
int a[1000005]={};
deque<int>q;
int main()
{
    n=read();
    for(int i=0;i<n;i++)a[i]=read();
    k=read();
    for(int i=0;i<n;i++)
    {
        while(!q.empty() && i-k+1>q.front())
        q.pop_front();
        while(!q.empty() && a[i]<a[q.back()])
        {
            q.pop_back();   
        }   
        q.push_back(i);
        if(i+1>=k)
        {
            cout<<a[q.front()]<<" ";
        }
    }
    q.clear();
    cout<<endl;
    for(int i=0;i<n;i++)
    {
        while(!q.empty() && i-k+1>q.front())
            q.pop_front();
        while(!q.empty() && a[i]>a[q.back()])
        {
            q.pop_back();   
        }   
        q.push_back(i);
        if(i+1>=k)
        {
            cout<<a[q.front()]<<" ";
        }
    }
    return 0;
}

|