题解:
二分高度,去算是否存在这样的高度出现再判断与m的大小关系
#include<bits/stdc++.h>
using namespace std;
long long n,m,maxx;
long long a[100005];
long long sum[100005];
long long h[100005];
long long t[100005];
long long read() {
long long sum=0;
char ch=getchar();
while(ch>'9'||ch<'0') {
ch=getchar();
}
while(ch>='0'&&ch<='9') {
sum=(sum<<3)+(sum<<1)+ch-'0';
ch=getchar();
}
return sum;
}
long long check(long long x) {
long long l=n,r=1;
for(long long i=1; i<=n; i++) {
while((x-(r-i)-a[r])>0&&r<=n)
r++;
t[i]=r;
}
for(long long i=n; i>=1; i--) {
while((x-(i-l)-a[l])>0&&l>=1)
l--;
h[i]=l;
}
for(long long i=1; i<=n; i++) {
if(h[i]==0||t[i]==n+1)continue;
long long tot=x*(t[i]-h[i]-1)-(t[i]-i)*(t[i]-i-1)/2-(i-h[i])*(i-h[i]-1)/2;;
tot-=sum[t[i]-1]-sum[h[i]];
if(tot<=m)return 1;
}
return 0;
}
int main() {
n=read();
m=read();
for(long long i=1; i<=n; i++) {
a[i]=read();
maxx=max(maxx,a[i]);
sum[i]=sum[i-1]+a[i];
}
long long l=1,r=400000000+maxx;
long long ans;
while(l<r) {
long long mid=(l+r)>>1;
if(check(mid)) {
l=mid+1;
ans=mid;
} else r=mid;
}
printf("%d",ans);
return 0;
}