|
| 1 | +/* |
| 2 | +Problem Name: Robin Hood in Town |
| 3 | +
|
| 4 | +Short Problem Statement: |
| 5 | +There are n people in a town, each having some amount of gold. |
| 6 | +The richest person finds an extra pot of gold containing x gold and adds it |
| 7 | +to their wealth. |
| 8 | +
|
| 9 | +A person is considered unhappy if their wealth is strictly less than |
| 10 | +half of the average wealth of all people. |
| 11 | +
|
| 12 | +If strictly more than half of the population becomes unhappy, |
| 13 | +Robin Hood appears. |
| 14 | +
|
| 15 | +The task is to determine the minimum value of x such that Robin Hood appears. |
| 16 | +If it is impossible, output -1. |
| 17 | +
|
| 18 | +Approach: |
| 19 | +1. If n <= 2, it is impossible for more than half of the people to be unhappy, |
| 20 | + so return -1. |
| 21 | +2. Sort the array of wealth values. |
| 22 | +3. Compute the initial total sum of wealth. |
| 23 | +4. Use Binary Search on x (extra gold added to the richest person): |
| 24 | + - For a candidate x, compute the new total wealth. |
| 25 | + - Count how many people have wealth strictly less than half of the average. |
| 26 | + - If unhappy people > n/2, the condition is satisfied. |
| 27 | +5. Minimize x using binary search. |
| 28 | +
|
| 29 | +Binary search works because increasing x increases the average wealth, |
| 30 | +which monotonically increases the number of unhappy people. |
| 31 | +
|
| 32 | +Time Complexity: |
| 33 | +O(n log M) per test case |
| 34 | +(where M is the search range for x, up to 1e18) |
| 35 | +
|
| 36 | +Space Complexity: |
| 37 | +O(n) |
| 38 | +
|
| 39 | +Question Link: |
| 40 | +https://codeforces.com/contest/2014/problem/C |
| 41 | +
|
| 42 | +Submission Link: |
| 43 | +https://codeforces.com/submissions/amansharma264 |
| 44 | +*/ |
| 45 | + |
| 46 | +#include <bits/stdc++.h> |
| 47 | +using namespace std; |
| 48 | + |
| 49 | +#define fastio() ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr) |
| 50 | +#define ll long long |
| 51 | +#define endl '\n' |
| 52 | + |
| 53 | +int main() { |
| 54 | + fastio(); |
| 55 | + |
| 56 | + ll t; |
| 57 | + cin >> t; |
| 58 | + while (t--) { |
| 59 | + ll n; |
| 60 | + cin >> n; |
| 61 | + |
| 62 | + vector<ll> a(n); |
| 63 | + for (ll i = 0; i < n; i++) { |
| 64 | + cin >> a[i]; |
| 65 | + } |
| 66 | + |
| 67 | + if (n <= 2) { |
| 68 | + cout << -1 << endl; |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + sort(a.begin(), a.end()); |
| 73 | + |
| 74 | + ll sum = 0; |
| 75 | + for (ll x : a) sum += x; |
| 76 | + |
| 77 | + ll low = 0, high = 1e18, ans = -1; |
| 78 | + |
| 79 | + while (low <= high) { |
| 80 | + ll mid = low + (high - low) / 2; |
| 81 | + ll totalSum = sum + mid; |
| 82 | + |
| 83 | + ll unhappy = 0; |
| 84 | + for (ll i = 0; i < n; i++) { |
| 85 | + if (2LL * n * a[i] < totalSum) { |
| 86 | + unhappy++; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (unhappy > n / 2) { |
| 91 | + ans = mid; |
| 92 | + high = mid - 1; |
| 93 | + } else { |
| 94 | + low = mid + 1; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + cout << ans << endl; |
| 99 | + } |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
0 commit comments