90分,求救!!!

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

linan__nanil @ 2024-07-20 19:18:04

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
deque<int> q;
long long a[maxn];
int n,k;
int main() 
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        if(q.front()<=i-k)q.pop_front();
        while(q.size()&&a[q.back()]>=a[i])q.pop_back();
        q.push_back(i);
        if(i>=k)cout<<a[q.front()]<<" ";
    }
    q.clear();
    cout<<endl;
    for(int i=1;i<=n;i++)
    {
        if(q.front()<=i-k)q.pop_front();
        while(q.size()&&a[q.back()]<=a[i])q.pop_back();
        q.push_back(i);
        if(i>=k)cout<<a[q.front()]<<" ";
    }
    return 0;
}

求助各位大佬,90分,RE一个点(蒟蒻勿喷)


by linan__nanil @ 2024-07-20 19:19:56

我就像个飞舞一样,模板都不会,www......


by Dagsn1452 @ 2024-07-23 16:45:00

@linan__nanil 你是废物我是啥....wuwu


by StarsIntoSea_SY @ 2024-07-24 10:32:53

@linan__nanil 17行和26行要加一个判断队列为空的情况。

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
deque<int> q;
long long a[maxn];
int n,k;
int main() 
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        if(!q.empty() && q.front()<=i-k)q.pop_front();
        while(q.size()&&a[q.back()]>=a[i])q.pop_back();
        q.push_back(i);
        if(i>=k)cout<<a[q.front()]<<" ";
    }
    q.clear();
    cout<<endl;
    for(int i=1;i<=n;i++)
    {
        if(!q.empty() && q.front()<=i-k)q.pop_front();
        while(q.size()&&a[q.back()]<=a[i])q.pop_back();
        q.push_back(i);
        if(i>=k)cout<<a[q.front()]<<" ";
    }
    return 0;
}

by linan__nanil @ 2024-07-25 19:15:05

@StarsIntoSea_SY,感谢大佬相助,膜拜!!!


|