File tree Expand file tree Collapse file tree
Problems/Binary-Search/Day-06/suzzzal Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments