只得了20,大佬求带!哪错了呀!
查看原帖
只得了20,大佬求带!哪错了呀!
963035
future_smile楼主2023/4/13 16:28
#include <iostream>
#include <cstring>
#include <algorithm>

typedef long long LL;

using namespace std;
LL n,a[1000010],m, s;

LL mid(LL m)
{
    
    LL midd=m,res=0;
    for (int i = 0; i < n; i++)
    {
        if (a[i] > m)
        {
            res += a[i] - m;
        }
    }
    return res;
}
int main()
{
    LL l, r;
    scanf("%lld%lld", &n, &s);
    for (LL i = 0; i < n; i++)
    {
        scanf("%lld", &a[i]);
    }
    sort(a, a + n);
    l = a[0];
    r = a[n-2];
    while (l <= r)
    {

        if (mid((l+r)/2) > s)
        {
            l = (l + r) / 2 + 1;
        }
        if (mid((l+r)/2) == s)
        {
            l = (l + r) / 2 ;
            break;
        }
        else
        {
            r = (l + r) / 2 - 1;
        }
    }
    printf("%lld",l);
    
    return 0;
}
2023/4/13 16:28
加载中...