Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
3.1 kB
3
Indexable
Never
//                                       Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define all(x) (x).begin(), (x).end()
#define asort(s) (sort(s.begin(), s.end()))
#define REV(v) (reverse(v.begin(), v.end()))
#define count_n(v, n) (count(v.begin(), v.end(), n))
#define max_v(a) (*max_element(a.begin(), a.end()))
#define t_sum(sv) (accumulate(sv.begin(), sv.end(), 0ll))
#define mini_v(a) (*min_element((a).begin(), (a).end()))
#define dsort(s) (sort(s.begin(), s.end(), greater<>()))
#define binary(b, value) (binary_search(b.begin(), b.end(), value))
#define uper_b(b, value) (upper_bound(b.begin(), b.end(), value))
#define Low_b(b, value) (lower_bound(b.begin(), b.end(), value))

#define print(x)              \
    for (auto &value : x)     \
    {                         \
        cout << value << " "; \
    }
#define sz(v) ((ll)(v).size())
#define cos(a) cos(a *pi / 180)
#define sin(a) sin(a *pi / 180)
#define tan(a) tan(a *pi / 180)
#define pi 3.141592654
#define pb push_back
#define w(n) while (n--)
#define f first
#define s second
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#pragma GCC optimize("Ofast")
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

void solution()
{
    ll n, sum = 0;
    cin >> n;
    vector<ll> v;
    for (ll i = 0; i < n; i++)
    {
        ll x;
        cin >> x;
        v.pb(x);
        sum += v[i];
    }
    asort(v);

    if (n == 1 || n == 2)
    {
        cout << "-1" << endl;
    }
    else if (n > 2)
    {
        double avg = (sum * 1.0);
        avg /= n;
        avg /= 2;
        ll cnt = 0;
        for (ll i = 0; i < n; i++)
        {
            if (v[i] < avg)
            {
                cnt++;
            }
        }

        if (cnt > (n / 2))
        {
            cout << 0 << endl;
            return;
        }
        else
        {
            ll l = 0, r = 1e12;
            cnt = 0;
            ll res = 0;
            while (l <= r)
            {
                ll mid = l + (r - l) / 2;
                double avg = (sum + mid * 1.0);

                avg /= n;
                avg /= 2;

                for (ll i = 0; i < n; i++)
                {
                    if (v[i] < avg)
                    {
                        cnt++;
                    }
                }

                if (cnt > (n / 2))
                {
                    res = mid;
                    r = mid - 1;
                    cnt = 0;
                }
                else
                {
                    l = mid + 1;
                    cnt = 0;
                }
            }
            cout << res << endl;
        }
        v.clear();
    }
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll t;
    cin >> t;
    w(t)
    {
        solution();
    }
    return 0;
}
Leave a Comment