砍树
大概是从这道题开始理解什么是真正的二分法,而不仅仅是通过不断对半分来搜索。
#include <bits/stdc++.h>
#define RG register int
#define rep(i,a,b) for(RG i=a;i<=b;++i)
#define per(i,a,b) for(RG i=a;i>=b;--i)
#define ll long long
#define inf (1<<29)
const int maxn=1e6+10;
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
ll a[maxn],n,m,le=0,mid,ri,sum;
int main()
{
scanf("%lld%lld",&n,&m);
for(ll i=1;i<=n;++i)scanf("%lld",&a[i]);//一开始都忘了设置i的初值
sort(a+1,a+1+n);
ri=a[n];
while(le<=ri)
{
sum=0;
mid=(le+ri)/2;
for(ll i=1;i<=n;++i)if(a[i]>mid)sum+=a[i]-mid;
if(sum<m)ri=mid-1;
else le=mid+1;
}
cout<<ri;
return 0;
}