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
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;
}