题目:Present
思路:
二分答案+贪心。
注意在LL范围类二分。
代码:
#include<bits/stdc++.h>
using namespace std;
#define maxn 100000
#define ll long long
int n,m,w;
int a[maxn*2+5]= {0};
ll tg[maxn*2+5]= {0};
bool judge(ll x) {
ll b=0,c=0;
memset(tg,0,sizeof(tg));
for(int i=1; i<=n; i++) {
b-=tg[i];
ll y=a[i]+b;
if(y<x) {
b+=x-y;
tg[i+w]+=x-y;
c+=x-y;
}
if(c>m) return 0;
}
return 1;
}
ll binsearch() {
ll l=0,r=1LL<<40;
while(l+1<r) {
ll mid=l+(r-l)/2;
if(judge(mid)) l=mid;
else r=mid;
}
if(judge(l+1)) return l+1;
if(!judge(l)) return l-1;
else return l;
}
int main() {
scanf("%d%d%d",&n,&m,&w);
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
ll ans=binsearch();
printf("%lld",ans);
return 0;
}