Skip to content

Commit 8e33d4f

Browse files
committed
added soluiton for day6 problem 1
1 parent d0fba44 commit 8e33d4f

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
3+
Problem : Robin Hood in Town 2014C
4+
5+
Approach : Used binary search to find the minimum amount that needs to be added to
6+
the richest person’s wealth to make Robin Hood appear.
7+
if(n<=2) not possible
8+
9+
Time Complexity : O(nlogn)
10+
Space Complexity : O(1) if input is not considered
11+
12+
Submission Link : https://codeforces.com/contest/2014/submission/355969335
13+
*/
14+
#include <bits/stdc++.h>
15+
#include <bits/stdc++.h>
16+
#include <ext/pb_ds/assoc_container.hpp>
17+
#include <ext/pb_ds/tree_policy.hpp>
18+
using namespace std;
19+
using namespace __gnu_pbds;
20+
21+
#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);
22+
#define int long long
23+
#define ull unsigned long long
24+
#define ld long double
25+
#define vi vector<int>
26+
#define vb vector<bool>
27+
#define vs vector<string>
28+
#define pii pair<int,int>
29+
#define pll pair<long long,long long>
30+
#define vpi vector<pii>
31+
#define vpl vector<pll>
32+
#define vvi vector<vi>
33+
#define mii map<int,int>
34+
#define si set<int>
35+
#define osi ordered_set<int>
36+
#define loop(i,n) for (int i = 0; i < n; i++)
37+
#define rloop(i,n) for (int i = n - 1; i >= 0; i--)
38+
#define loop1(i,a,b) for (int i = a; i <= b; i++)
39+
#define rloop1(i,a,b) for (int i = a; i >= b; i--)
40+
#define pb push_back
41+
#define ppb pop_back
42+
#define mp make_pair
43+
#define F first
44+
#define S second
45+
#define lb lower_bound
46+
#define ub upper_bound
47+
#define all(x) (x).begin(), (x).end()
48+
#define rall(x) (x).rbegin(), (x).rend()
49+
#define sz(x) ((int)(x).size())
50+
#define findbo(x) find_by_order(x) // returns iterator to the k-th largest element (0-based)
51+
#define orderbk(x) order_of_key(x) // returns number of items strictly smaller than x
52+
#define endl '\n'
53+
#define printy cout<<"YES"<<endl
54+
#define printn cout<<"NO"<<endl
55+
#define vout(a) for (int i = 0; i < a.size(); i++) cout << a[i] << ' '; cout << endl;
56+
#define vpout(a) loop(i,sz(a)) cout <<a[i].F<<' '<< a[i].S<< endl; cout << endl;
57+
#define rt(x){ cout<<x<<endl; return; }
58+
template<typename T>
59+
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // less_equal<T> for multiset, greater<T> for decreasing order
60+
61+
const int MOD = 1e9 + 7;
62+
const int INF = 1e9;
63+
64+
#ifndef ONLINE_JUDGE
65+
#define debug(x) cerr<<#x<<" = "; _print(x); cerr<<endl;
66+
template<typename T> void _print(const T& x){ cerr<<x; }
67+
template<typename T,typename U> void _print(const pair<T,U>& p){ cerr<<"("; _print(p.first); cerr<<", "; _print(p.second); cerr<<")"; }
68+
template<typename T> void _print(const vector<T>& v){ cerr<<"["; for(size_t i=0;i<v.size();++i){ _print(v[i]); if(i+1!=v.size()) cerr<<", "; } cerr<<"]"; }
69+
template<typename T> void _print(const set<T>& s){ cerr<<"{"; for(auto it=s.begin(); it!=s.end(); ++it){ _print(*it); if(next(it)!=s.end()) cerr<<", "; } cerr<<"}"; }
70+
template<typename T,typename U> void _print(const map<T,U>& m){ cerr<<"{"; for(auto it=m.begin(); it!=m.end(); ++it){ _print(it->first); cerr<<": "; _print(it->second); if(next(it)!=m.end()) cerr<<", "; } cerr<<"}"; }
71+
#else
72+
#define debug(x)
73+
#endif
74+
75+
#define sumV(a) accumulate(all(a), 0LL)
76+
#define minV(a) min_element(all(a))
77+
#define maxV(a) max_element(all(a))
78+
79+
int log2f(long long n) {
80+
if (n==0) return -1;
81+
int res = 0;
82+
while (n > 1){
83+
n >>= 1;
84+
res++;
85+
}
86+
return res;
87+
}
88+
89+
int binpow(int a, int n) {
90+
int result = 1;
91+
a %= MOD;
92+
while (n > 0) {
93+
if (n & 1)
94+
result = (result * a) % MOD;
95+
a = (a * a) % MOD;
96+
n >>= 1;
97+
}
98+
return result;
99+
}
100+
101+
int check(int m, int sum, vi a){
102+
int n = a.size(), ind, mx = -1;
103+
loop(i,n){
104+
if(a[i]>mx){
105+
mx = a[i];
106+
ind = i;
107+
}
108+
}
109+
sum+=m;
110+
int cnt = 0;
111+
loop(i,n){
112+
if(i!=ind && 2*n*a[i]<sum) cnt++;
113+
}
114+
return (cnt > n/2);
115+
}
116+
117+
void solve(){
118+
int n;
119+
cin>>n;
120+
vi a(n);
121+
loop(i,n) cin>>a[i];
122+
123+
int sum = accumulate(all(a),0LL);
124+
125+
if(n<3) {rt(-1)}
126+
127+
int l = 0, h = INT64_MAX, ans;
128+
while(h-l>1){
129+
int mid = (l+h)/2;
130+
if(check(mid,sum,a)) h = mid;
131+
else l = mid + 1;
132+
}
133+
134+
if(check(l,sum,a)) ans = l;
135+
else ans = h;
136+
137+
cout<<ans<<endl;
138+
}
139+
140+
int32_t main(){
141+
fast_io;
142+
int t=1;
143+
cin>>t;
144+
while(t--){
145+
solve();
146+
}
147+
return 0;
148+
}

0 commit comments

Comments
 (0)