-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path122-water-the-trees.cpp
More file actions
60 lines (59 loc) · 1.47 KB
/
Copy path122-water-the-trees.cpp
File metadata and controls
60 lines (59 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Water the Trees [1661C]
* Problem: https://codeforces.com/problemset/problem/1661/C
* Verdict: ACCEPTED Solved: 2024-03-05
* Language: C++20 (GCC 11-64)
* Runtime: 264 ms Memory: 4700 KB
* Tags: binary search, greedy, math
* Author: BidoTeima
* Source: https://codeforces.com/contest/1661/submission/249737709
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int ok(ll lim, vector<ll>a, int n){
ll sm = 0, mx = *max_element(a.begin(), a.end());
ll l = lim;
for(int i = 0; i < n; i++){
int d = min(lim, (mx - a[i]))/2;
lim -= 2 * d;
sm += mx - a[i] - 2 * d;
}
if(2 * sm - 1 <= l) return 1;
lim = l, sm = 0, mx++;
for(int i = 0; i < n; i++){
int d = min(lim, (mx - a[i]))/2;
lim -= 2 * d;
sm += mx - a[i] - 2 * d;
}
return 2 * sm - 1 <= l;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<ll>a(n);
for(auto&i:a)cin>>i;
sort(a.begin(),a.end());
if(a[0]==a[n-1]){
cout<<"0\n";
continue;
}
ll lo = 1, hi = 1e18, ans = hi, mid;
while(lo <= hi){
mid = (lo + hi) >> 1;
if(ok(mid, a, n)){
ans = mid;
hi = mid - 1;
}
else lo = mid + 1;
}
cout<<ans<<'\n';
}
return 0;
}