-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path320-trinity.cpp
More file actions
62 lines (57 loc) · 1.57 KB
/
Copy path320-trinity.cpp
File metadata and controls
62 lines (57 loc) · 1.57 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
61
62
/*
* Trinity [2032C]
* Problem: https://codeforces.com/problemset/problem/2032/C
* Verdict: ACCEPTED Solved: 2025-03-02
* Language: C++17 (GCC 7-32)
* Runtime: 109 ms Memory: 0 KB
* Tags: binary search, math, sortings, two pointers
* Author: BidoTeima
* Source: https://codeforces.com/contest/2032/submission/308710626
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
for(int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
if(a[0] + a[1] > a[n - 1]){
cout<<"0\n";
continue;
}
//cout<<'\n';
int ans = n;
for(int i = 0; i < n; i++){
// set a_[0, i - 1] = a[i]
// set a_[res + 1, n - 1] = a[res]
int lo = i, hi = n - 1, mid, res = i;
while(lo <= hi){
mid = (lo + hi) >> 1;
if(a[i] + a[i] > a[mid]){
lo = mid + 1;
res = mid;
} else hi = mid - 1;
}
ans = min(ans, i + n - res - 1);
if(i==0)continue;
lo=i,hi=n-1,mid,res=i;
while(lo <= hi){
mid = (lo + hi) >> 1;
if(a[i - 1] + a[i] > a[mid]){
lo=mid+1;res=mid;
} else hi=mid-1;
}
ans=min(ans,i-1+n-res-1);
}
cout << ans << '\n';
}
return 0;
}