4385: [POI2015]Wilcze doły|单调队列

单调队列的题怎么总感觉不是很好想..
枚举右区间,维护一个长度为d的区间使其区间和单调递减并且顺序是从左到右

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define ll long long
#define N 2000005
using namespace std;
ll sc()
{
    ll i=0; char c=getchar();
    while(c>'9'||c<'0')c=getchar();
    while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar();
    return i;
} 
ll a[N],p;
int n,d,q[N],ans;
int main()
{
    n=sc(),p=sc(),d=sc();
    for(int i=1;i<=n;i++)a[i]=a[i-1]+sc();
    int l=1,r=0,st=0;ans=d;
    for(int i=d;i<=n;i++)
    {
        while(l<=r&&a[q[r]]-a[q[r]-d]<a[i]-a[i-d])r--;
        q[++r]=i;
        while(a[i]-a[st]-a[q[l]]+a[q[l]-d]>p)
        {
            st++;
            if(q[l]-d<st)l++;
        }
        //cout << i<<" "<< st<< endl;
        ans=max(ans,i-st);
    }
    cout<<ans;
    return 0;
}

你可能感兴趣的:(单调队列)