Skip to content

Commit c312f38

Browse files
committed
Solved Issue #405: Robin Hood in Town (Binary Search)
1 parent beb4f03 commit c312f38

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Problem: Robin Hood in Town
3+
Link: https://codeforces.com/contest/2014/problem/C
4+
Author: Krishna Sikheriya (Krishna200608)
5+
6+
Short Problem Statement:
7+
We have a population of n people with given wealths. We want to give x gold to one person
8+
(the richest) such that strictly more than half of the population becomes "unhappy".
9+
A person is unhappy if their wealth is strictly less than half of the average wealth.
10+
Find the minimum non-negative integer x to achieve this. If impossible, output -1.
11+
12+
Approach:
13+
We need to find the minimum integer x such that the count of people with wealth < (new_average / 2) is > n / 2.
14+
1. If n <= 2, it is impossible to have strictly more than half the population satisfy the condition (since the max wealth will always be >= average). Output -1.
15+
2. Sort the wealth array to easily count or check conditions.
16+
3. The condition is monotonic: adding more gold increases the average, making it easier for existing wealths to fall below half the average.
17+
4. We can Binary Search for x in the range [0, 1e18].
18+
- Calculation: New Sum = Current Sum + mid.
19+
- Check: We need strictly more than n/2 people to be poor.
20+
In a sorted array, this means the person at index n/2 (0-indexed) must be poor.
21+
Condition: a[n/2] < (Sum + mid) / (2 * n)
22+
To avoid floating point errors, we use cross-multiplication:
23+
2 * n * a[n/2] < Sum + mid
24+
5. The smallest valid 'mid' is our answer.
25+
26+
Time Complexity: O(n log n) due to sorting. Binary search takes O(log(1e18)) steps, each check is O(1).
27+
Space Complexity: O(n) to store the wealths.
28+
29+
Example I/O:
30+
Input:
31+
2
32+
1
33+
1
34+
3
35+
2 3 3
36+
37+
Output:
38+
-1
39+
3
40+
*/
41+
42+
#include <iostream>
43+
#include <vector>
44+
#include <algorithm>
45+
#include <numeric>
46+
47+
using namespace std;
48+
49+
// Use long long to prevent overflow
50+
using ll = long long;
51+
52+
void solve() {
53+
int n;
54+
if (!(cin >> n)) return;
55+
56+
vector<ll> a(n);
57+
ll sum = 0;
58+
for (int i = 0; i < n; ++i) {
59+
cin >> a[i];
60+
sum += a[i];
61+
}
62+
63+
// If n is 1 or 2, it's impossible to have >50% population strictly less than half average
64+
if (n <= 2) {
65+
cout << -1 << "\n";
66+
return;
67+
}
68+
69+
sort(a.begin(), a.end());
70+
71+
// We binary search for x. Range [0, 2e18] is sufficient.
72+
ll low = 0, high = 2e18;
73+
ll ans = -1;
74+
75+
while (low <= high) {
76+
ll mid = low + (high - low) / 2;
77+
78+
// Check condition: a[n/2] < (sum + mid) / (2 * n)
79+
// Equivalent to: 2 * n * a[n/2] < sum + mid
80+
if (2LL * n * a[n/2] < sum + mid) {
81+
ans = mid;
82+
high = mid - 1; // Try smaller x
83+
} else {
84+
low = mid + 1; // Need larger x
85+
}
86+
}
87+
88+
cout << ans << "\n";
89+
}
90+
91+
int main() {
92+
ios_base::sync_with_stdio(false);
93+
cin.tie(NULL);
94+
95+
int t;
96+
if (cin >> t) {
97+
while (t--) {
98+
solve();
99+
}
100+
}
101+
return 0;
102+
}
103+
104+
/*
105+
SUBMISSION LINK:
106+
https://codeforces.com/contest/2014/submission/356082645
107+
*/

0 commit comments

Comments
 (0)