You are given an array aa of nn elements a1,a2,…,ana1,a2,…,an.
You can perform the following operation any number (possibly 00) of times:
- Choose two integers ii and jj, where 1≤i,j≤n1≤i,j≤n, and assign ai:=ajai:=aj.
Find the minimum number of operations required to make the array aa satisfy the condition:
- For every pairwise distinct triplet of indices (x,y,z)(x,y,z) (1≤x,y,z≤n1≤x,y,z≤n, x≠yx=y, y≠zy=z, x≠zx=z), there exists a non-degenerate triangle with side lengths axax, ayay and azaz, i.e. ax+ay>azax+ay>az, ay+az>axay+az>ax and az+ax>ayaz+ax>ay.
Input
Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer nn (3≤n≤2⋅1053≤n≤2⋅105) — the number of elements in the array aa.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array aa.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output a single integer — the minimum number of operations required.
Examples
Inputcopy | Outputcopy |
---|---|
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 | 3 1 0 8 |
Note
In the first test case, one of the possible series of operations would be:
- Assign a1:=a4=4a1:=a4=4. The array will become [4,2,3,4,5,6,7][4,2,3,4,5,6,7].
- Assign a2:=a5=5a2:=a5=5. The array will become [4,5,3,4,5,6,7][4,5,3,4,5,6,7].
- Assign a7:=a1=4a7:=a1=4. The array will become [4,5,3,4,5,6,4][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 33 operations.
In the second test case, we can assign a1:=a2=3a1:=a2=3 to make the array a=[3,3,2]a=[3,3,2].
In the third test case, since 33, 44 and 55 are valid side lengths of a triangle, we don't need to perform any operation to the array.
你需要把a变成好数组,即任意三个不同下标,对应的元素可以组成一个三角形(两边之和大于第三边)。输出最小操作次数。
#include<bits/stdc++.h>
#define int long long
#define N 1000005
#define INF 0x3f3f3f3f
using namespace std;
int a[N];
signed main()
{
int t;
cin>>t;
while(t--)
{
int ans=INF;
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=3;i<=n;i++)
{
int ub=upper_bound(a+1,a+n+1,a[i])-a; //找到第一个比a[i]大的位置
int l=1,r=i-2;
while(l<r)
{
int mid=(l+r+1)>>1;
if(a[mid]+a[mid+1]<=a[i])
l=mid;
else r=mid-1;
}
ans=min(ans,n-ub+1+(a[r]+a[r+1]<=a[i]?r:0));
}
cout<<ans<<endl;
}
return 0;
}