http://codeforces.com/problemset/problem/954/G
二分答案
检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越好
差分即可
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; #define N 500001 typedef long long LL; int n,r;
LL k; LL sum[N];
LL cf[N]; template<typename T>
void read(T &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} bool check(LL x)
{
LL rest=k,need;
LL add=;
memset(cf,,sizeof(cf));
for(int i=;i<=n;++i)
{
add+=cf[i];
if(sum[i]+add<x)
{
need=x-sum[i]-add;
rest-=need;
if(rest<) return false;
cf[i+]+=need;
if(i+*r<n) cf[i+*r+]-=need;
}
}
return true;
} int main()
{
read(n); read(r); read(k);
int x;
for(int i=;i<=n;++i)
{
read(x);
sum[max(,i-r)]+=x;
if(i+r<n) sum[i+r+]-=x;
}
for(int i=;i<=n;++i) sum[i]+=sum[i-];
LL l=,r=2e18,mid,ans=;
while(l<=r)
{
mid=l+r>>;
if(check(mid)) ans=mid,l=mid+;
else r=mid-;
}
cout<<ans;
}