P3594 [POI2015] WIL-pitfalls

————————————————————————————————————————————————————

Modified once the monotony of the queue, the queue is determined by means of monotonous reach the maximum interval delete, and then by means of a continuous growth of l, r to solve, very good question

——————————————————————————————————————————————————

#include<bits/stdc++.h>
using namespace std;
long long a[2000005],n,p,d,sum[2000005],q[2000005],last=1,hd,tl,t[2000005],ans;
int main()
{
    cin>>n>>p>>d;
    for(int i=1;i<=n;i++){cin>>a[i];sum[i]=sum[i-1]+a[i];}
    for(int i=d;i<=n;i++)t[i]=sum[i]-sum[i-d];
    ans=d;q[++tl]=d;
    for(int i=d+1;i<=n;i++)
    {
        while(hd<=tl&&t[q[tl]]<t[i])tl--;
        q[++tl]=i;
        while(hd<=tl&&q[hd]-d+1<last)hd++;
        while(hd<=tl&&sum[i]-sum[last-1]-t[q[hd]]>p)
        {
            last++;
            while(hd<=tl&&q[hd]-d+1<last)hd++;
        }
        ans=max(i-last+1,ans);
    }
    cout<<ans;
}

 

Guess you like

Origin www.cnblogs.com/SFWR-YOU/p/11402662.html