You are given an array 𝑎 of 𝑛 elements 𝑎1,𝑎2,…,𝑎𝑛. You can perform the following…
- User
You are given an array 𝑎 of 𝑛 elements 𝑎1,𝑎2,…,𝑎𝑛.
You can perform the following operation any number (possibly 0) of times:
Choose two integers 𝑖 and 𝑗, where 1≤𝑖,𝑗≤𝑛, and assign 𝑎𝑖:=𝑎𝑗. Find the minimum number of operations required to make the array 𝑎 satisfy the condition:
For every pairwise distinct triplet of indices (𝑥,𝑦,𝑧) (1≤𝑥,𝑦,𝑧≤𝑛, 𝑥≠𝑦, 𝑦≠𝑧, 𝑥≠𝑧), there exists a non-degenerate triangle with side lengths 𝑎𝑥, 𝑎𝑦 and 𝑎𝑧, i.e. 𝑎𝑥+𝑎𝑦>𝑎𝑧, 𝑎𝑦+𝑎𝑧>𝑎𝑥 and 𝑎𝑧+𝑎𝑥>𝑎𝑦. Input Each test consists of multiple test cases. The first line contains a single integer 𝑡 (1≤𝑡≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer 𝑛 (3≤𝑛≤2⋅105) — the number of elements in the array 𝑎.
The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109) — the elements of the array 𝑎.
It is guaranteed that the sum of 𝑛 over all test cases does not exceed 2⋅105.
Output For each test case, output a single integer — the minimum number of operations required.
Example inputCopy 4 7 1 2 3 4 5 6 7 3 1 3 2 3 4 5 3 15 9 3 8 1 6 5 3 8 2 1 4 2 9 4 7 outputCopy 3 1 0 8 Note In the first test case, one of the possible series of operations would be:
Assign 𝑎1:=𝑎4=4. The array will become [4,2,3,4,5,6,7]. Assign 𝑎2:=𝑎5=5. The array will become [4,5,3,4,5,6,7]. Assign 𝑎7:=𝑎1=4. The array will become [4,5,3,4,5,6,4]. It can be proven that any triplet of elements with pairwise distinct indices in the final array forms a non-degenerate triangle, and there is no possible answer using less than 3 operations.
In the second test case, we can assign 𝑎1:=𝑎2=3 to make the array 𝑎=[3,3,2].
In the third test case, since 3, 4 and 5 are valid side lengths of a triangle, we don't need to perform any operation to the array. This solution is wrong #include #include #include
using namespace std; typedef long long ll; #define endl '\n'
void solve() { int n; cin >> n; vector a(n); for(auto &x: a) cin >> x; sort(a.begin(), a.end()); int max_a = a[n-1]; int a1 = a[0]; int a2 = a[1]; if((long long)a1 + a2 > max_a){ cout << "0\n"; return; }
int m = (max_a +1) /2; if( (max_a +1) %2 !=0){ m += 1; }
int cnt =0; for(auto x : a){ if(x < m) cnt++; } cout << cnt << "\n";
}
int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) solve(); } You need to consider cases where a_i + a_j = a_k, but a_i = a_j
- Assistant