Skip to content

Commit c978dfb

Browse files
authored
Merge pull request #422 from suzzzal/hhh
day06sol01
2 parents a99eb3d + 80a857b commit c978dfb

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// submission - https://codeforces.com/contest/2014/submission/356012129
2+
3+
/*
4+
Approach:
5+
Binary search the extra gold x.
6+
For each x, compute new average and count people with wealth < half of it.
7+
If count > n/2, x is valid. Find minimum such x.
8+
9+
Complexity:
10+
O(n log n)
11+
*/
12+
13+
#include<bits/stdc++.h>
14+
using namespace std;
15+
16+
int main(){
17+
int t;cin>>t;
18+
while(t--){
19+
int n;cin>>n;
20+
vector<long long>a(n);
21+
long long sum=0;
22+
for(int i=0;i<n;i++){cin>>a[i];sum+=a[i];}
23+
if(n<=2){cout<<-1<<"\n";continue;}
24+
sort(a.begin(),a.end());
25+
long long l=0,r=1e18,ans=-1;
26+
while(l<=r){
27+
long long mid=(l+r)/2;
28+
long double avg=(long double)(sum+mid)/n;
29+
long double half=avg/2.0;
30+
int cnt=0;
31+
for(long long v:a) if((long double)v<half) cnt++;
32+
if(cnt>n/2){ans=mid;r=mid-1;}
33+
else l=mid+1;
34+
}
35+
cout<<ans<<"\n";
36+
}
37+
return 0;
38+
}

0 commit comments

Comments
 (0)