思想:二分答案
为了寻找满足题意的锯子高度,应该在最低的树和最高的树之间进行二分答案;
排序寻找最大值和最小值,作为左边界和右边界,因为要寻找尽可能高的位置,所以寻找右边界,mid=l+r+1>>1(mid=(l+r+1)/2);
每次累加锯下来的高度,如果比所需大或者等于左边界,mid=左边界,否则mid=右边界-1;
输出r,即可
#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int N=1e6+5;
typedef pair<int,int> PII;
const int INF=0x3f3f3f3f;
typedef struct node{
}node;
void IOS(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
}
//试试upper_bound(),lower_bound();
int main(){
IOS();
ll n,m;
cin>>n>>m;
vector<ll>v(n);
for(int i=0;i<n;++i){
cin>>v[i];
}
sort(v.begin(),v.end());
ll l=v[0],r=v[n-1];
while(l<r){
ll cnt=0;
ll mid=l+r+1>>1;
for(int i=0;i<n;++i){
if(v[i]-mid>0){
cnt+=v[i]-mid;
}
}
if(cnt>=m){
l=mid;
}
else{
r=mid-1;
}
}
cout<<r;
return 0;
}