【bzoj4385】 [POI2015]Wilcze doły

Description

给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0。
请找到最长的一段连续区间,使得该区间内所有数字之和不超过p。

Input

第一行包含三个整数n,p,d(1<=d<=n<=2000000,0<=p<=10^16)。
第二行包含n个正整数,依次表示序列中每个数w[i](1<=w[i]<=10^9)。

Output

包含一行一个正整数,即修改后能找到的最长的符合条件的区间的长度。

Sample Input

9 7 2
3 4 1 9 4 1 7 1 3

Sample Output

5

HINT

将第4个和第5个数修改为0,然后可以选出区间[2,6],总和为4+1+0+0+1=6。


修改一定是d个全部变成0,单调队列维护最大的d个连续的数的和。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#define LL long long
using namespace std;
const int N=2000005;
int n,d,tot,tow,l,wz[N],ans,x;
LL p,sum[N],q[N],tmp;
int main (){
    scanf ("%d%lld%d",&n,&p,&d);
    for (int i=1;i<=n;++i){
        scanf ("%d",&x);
        sum[i]=sum[i-1]+x;
    }
    l=1;tow=-1;
    for (int i=d;i<=n;++i){
        for (tmp=sum[i]-sum[i-d];tot<=tow && q[tow]<=tmp;--tow);
        q[++tow]=tmp;wz[tow]=i;
        tmp=q[tot];
        while (sum[i]-sum[l-1]-tmp>p){
            if (l==wz[tot]-d+1)tot++;
            ++l;
        }
        ans=max(ans,i-l+1);
    }
    printf ("%d",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值