Input
5
6
1 7 7 9 9 9
6
5 4 2 1 30 60
9
14 19 37 59 1 4 4 98 73
1
2
6
3 10 1 17 15 11
Output
0
2
13
0
1
解析:
nlogn,二分答案。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
long long t,n,a[N];
bool check(long long x){
long long cnt=1,p=a[1]+2*x;
for(int i=1;i<=n;i++){
if(a[i]<=p) continue;
else{
cnt++;
p=a[i]+2*x;
}
}
return cnt<=3;
}
int main(){
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
if(n<=3){
cout<<0<<endl;
continue;
}
sort(a+1,a+n+1);
int l=0,r=1e9;
while(l<r){
int mid=l+r>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
printf("%lld\n",l);
}
return 0;
}