codeforces 1850E

一道很板的二分,需要注意数据溢出,开个_int128就行,注意,不要用pow,不要用pow,不要用pow!会有精度问题!

#include 
#include 
#include 
#include 

using namespace std;

using ll = long long;

const int M = 2e5 + 9;
ll s[M];
ll n, c;

bool check(ll x) {//二分找边框值
    __int128 sp = 0;
    for (int i = 1;i <= n;i++) {
        sp += (s[i] + x + x) * (s[i] + x + x);
    }
    return sp >= c;
}

void solve()
{
    cin >> n >> c;
    for (int i = 0;i < M;i++)s[i] = 0;
    ll j, k;
    j = 0;
    k = sqrt(c) + 1;
    for (int i = 1;i <= n;i++) {
        cin >> s[i];
    }
    // k = (c - m * n) / n;
    while (j < k) {
        ll mid = j + k >> 1;
        if (check(mid))k = mid;
        else j = mid + 1;
    }
    cout << k << '\n';
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

你可能感兴趣的:(codeforces,板刷二分,rating,1200,算法,c++,数据结构)