Skip to content

Commit 3fb9ca1

Browse files
Fixed the Code.
1 parent c312f38 commit 3fb9ca1

1 file changed

Lines changed: 33 additions & 65 deletions

File tree

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,75 @@
11
/*
22
Problem: Robin Hood in Town
33
Link: https://codeforces.com/contest/2014/problem/C
4-
Author: Krishna Sikheriya (Krishna200608)
4+
Author: Krishna200608
55
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.
6+
Short Problem Statement:=>
7+
Find minimum x to add to the richest person so that strictly more than half the population
8+
has wealth less than half the average.
119
1210
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.
11+
If n <= 2, it's impossible (-1).
12+
Sort the array. The target person to become "poor" is at index n/2.
13+
Binary Search for x in range [0, 1e18].
14+
Condition: 2 * n * a[n/2] < current-sum + x.
2515
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
16+
Time : O(N log N)
17+
Space Complexity: O(1)
4018
*/
4119

42-
#include <iostream>
43-
#include <vector>
44-
#include <algorithm>
45-
#include <numeric>
20+
/*
21+
Submission Link:
22+
https://codeforces.com/contest/2014/submission/356175570
23+
*/
4624

25+
#include <bits/stdc++.h>
4726
using namespace std;
48-
49-
// Use long long to prevent overflow
5027
using ll = long long;
28+
#define in(a) for(int i = 0; i<a.size(); i++) cin>>a[i];
29+
typedef vector<int> vi;
30+
typedef vector<ll> vll;
5131

5232
void solve() {
5333
int n;
54-
if (!(cin >> n)) return;
34+
cin >> n;
35+
vll a(n);
5536

56-
vector<ll> a(n);
5737
ll sum = 0;
58-
for (int i = 0; i < n; ++i) {
38+
for(int i = 0; i < n; i++) {
5939
cin >> a[i];
6040
sum += a[i];
6141
}
6242

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";
43+
if(n <= 2) {
44+
cout << "-1\n";
6645
return;
6746
}
6847

6948
sort(a.begin(), a.end());
7049

71-
// We binary search for x. Range [0, 2e18] is sufficient.
72-
ll low = 0, high = 2e18;
50+
ll l = 0, r = 1e18;
7351
ll ans = -1;
7452

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) {
53+
while(l <= r) {
54+
ll mid = l + (r - l) / 2;
55+
if(2LL * n * a[n/2] < sum + mid) {
8156
ans = mid;
82-
high = mid - 1; // Try smaller x
57+
r = mid - 1;
8358
} else {
84-
low = mid + 1; // Need larger x
59+
l = mid + 1;
8560
}
8661
}
87-
8862
cout << ans << "\n";
8963
}
9064

9165
int main() {
92-
ios_base::sync_with_stdio(false);
66+
ios::sync_with_stdio(false);
9367
cin.tie(NULL);
94-
9568
int t;
96-
if (cin >> t) {
97-
while (t--) {
98-
solve();
99-
}
69+
cin >> t;
70+
while(t--) {
71+
solve();
10072
}
10173
return 0;
10274
}
10375

104-
/*
105-
SUBMISSION LINK:
106-
https://codeforces.com/contest/2014/submission/356082645
107-
*/

0 commit comments

Comments
 (0)