10pts,蒟蒻救助

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

wangxinyu5483 @ 2024-10-08 19:40:36

求助!!!10pts

#include<bits/stdc++.h>
using namespace std;
long long a[10000000],n,k,temp;
deque<long long>q;
int main()
{
    scanf("%lld%lld",&n,&k);
    for(long long i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    temp=0;
    for(long long i=1;i<=n;i++)
    {
        while(!q.empty()&&q.back()>=a[i])
        {
            q.pop_back();
        }
        q.push_back(a[i]);
        if(i-temp>=k&&a[temp]==q.front())
        {
            temp++;
            q.pop_front();
        }
        if(i-temp>=k&&a[temp]!=q.front())
        {
            temp++;
        }
        if(i>=k-1)
        {
            cout<<q.front()<<' ';
        }
    }
    printf("\n");
    q.clear();
    temp=0;
    for(long long i=1;i<=n;i++)
    {
        while(!q.empty()&&q.back()<=a[i])
        {
            q.pop_back();
        }
        q.push_back(a[i]);
        if(i-temp>=k&&a[temp]==q.front())
        {
            temp++;
            q.pop_front();
        }
        if(i-temp>=k&&a[temp]!=q.front())
        {
            temp++;
        }
        if(i>=k-1)
        {
            cout<<q.front()<<' ';
        }
    }
    return 0;
}

by lch1218 @ 2024-10-19 21:31:11

额,你这个怎么说呢,看代码吧

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
int a[1000010],b[1000010],c[1000010];
deque<int> q;
deque<int> dq;
signed main(){
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        while(!q.empty() && a[q.back()]>a[i])   q.pop_back();
        q.push_back(i);
        if(i>k){
            if(i-k+1>q.front()){
                q.pop_front();
            }
        }
        if(i>=k)    b[i]=a[q.front()];
    }
    for(int i=1;i<=n;i++){
        while(!dq.empty() && a[dq.back()]<a[i]) dq.pop_back();
        dq.push_back(i);
        if(i>k){
            if(i-k+1>dq.front()){
                dq.pop_front();
            }
        }
        if(i>=k)    c[i]=a[dq.front()];
    }
    for(int i=k;i<=n;i++)   cout<<b[i]<<' ';
    cout<<endl;
    for(int i=k;i<=n;i++)   cout<<c[i]<<' ';
    return 0;
}

by lch1218 @ 2024-10-19 21:31:38

当然,你可以用clear


|