砍树TLE了QAQ
查看原帖
砍树TLE了QAQ
1227680
zhajige124869楼主2024/3/31 10:45
#include <bits/stdc++.h>
using namespace std;
int a[1000005];
bool cmp(int i1, int i2){
    return i1 > i2;
}
int main(){
    int n,M;
    cin >> n >>M;
    for (int i = 0;i < n;++i) cin >> a[i];
    sort(a,a+n,cmp);
    int l = 0,r = 1000000000;
    while (l <= r){
        int  mid = l+(r-1)/2;
      long long  tot = 0;
        for (int i = 0; i < n; i++){
            if (a[i] > mid ) tot+= a[i] - mid;
            else break;
        }
        if (tot > M)  l = mid + 1;
        else if (tot < M)  	r = mid - 1;       
		else {
            cout << mid;
            return 0;
        }
    }
    cout << r;
    return 0;
}
2024/3/31 10:45
加载中...