题解
观察到求最大值,所以尝试二分
观察样例规则发现,如果 \(a_i\) 达到 \(x\),则 \(a_{i+1}\) 至少达到 \(x-1\)
code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=114514;
ll n,k;
ll a[N]={0};
ll b[N]={0};
bool check(ll h)
{
for(int i=1;i<=n;i++)
{
ll tem=0,need=h;
bool flag=0;
for(int j=i;j<=n;j++)
{
if(need<=a[j])
{
flag=1;
break;
}
else
{
tem+=need-a[j];
need--;
}
}
if(flag&&tem<=k) return 1;
}
return 0;
}
void solve()
{
cin>>n>>k;
ll maxs=0;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
ll l=1,r=2e8;
while(l+1<r)
{
ll mid=(l+r)/2;
if(check(mid)) l=mid;
else r=mid;
}
cout<<l<<'\n';
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--) solve();
return 0;
}
标签:题解,ll,long,while,Max,Become
From: https://www.cnblogs.com/pure4knowledge/p/18306627