70分代码求助
查看原帖
70分代码求助
1275181
code_AZ楼主2024/3/4 21:51
#include<iostream>
using namespace std;
const int n = 1000010;
int tree_number, tree_long;
int hight_array[n];
int j, result=0;
void quick_sort(int q[], int l, int r)
{
    if (l >= r) return;

    int i = l - 1, j = r + 1, x = q[(l + r )/2 ];
    while (i < j)
    {
        do i++; while (q[i] < x);
        do j--; while (q[j] > x);
        if (i < j) swap(q[i], q[j]);
    }
    quick_sort(q, l, j), quick_sort(q, j + 1, r);
}
bool check(int x) 
{
    result = 0;
    for (j = 0; j < tree_number; j++)
    {
        if (x < hight_array[j])
            result = result + (hight_array[j] - x);
    }
    if (result>=tree_long) return true;
    else return false;
} 
int bsearch_2(int l, int r)
{
    while (l < r)
    {
        int mid = (l + r + 1) / 2;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    return l;
}
int main ()
{
    cin>> tree_number>>tree_long;
    for (int i = 0; i <tree_number; i++)
    {
        cin>>hight_array[i];
    }
    quick_sort(hight_array, 0, tree_number - 1);
    if (tree_number >= 1)
    {
        int k = tree_number - 1;
        int left = 0, right = hight_array[k];
        cout << bsearch_2(left, right);
    }
    return 0;
}
2024/3/4 21:51
加载中...