P1873 [COCI 2011/2012 #5] EKO / 砍树

点击查看代码
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N=1e6+10;
int n;
LL m;
int h[N];

bool check(int x)
{
    LL sum=0;
    for(int i=1;i<=n;i++){
        if(h[i]>x){
            sum+=h[i]-x;
        }
    }

    return sum>=m;
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);

    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>h[i];

    int max_h=0;
    for(int i=1;i<=n;i++){
        if(h[i]>max_h) max_h=h[i];
    }

    int l=0,r=max_h;
    int ans=0;
    while(l<=r){
        int mid=l+(r-l)/2;
        if(check(mid)){
            ans=mid;
            l=mid+1;
        }else{
            r=mid-1;
        }
    }

    cout<<ans<<endl;
    
}

posted @ 2026-01-28 20:03  AnoSky  阅读(5)  评论(0)    收藏  举报