题目链接
题目大意
给你n(n<=100)个点,要你找一个点使得和所有点距离的最大值最小值ans
题目思路
一直在想二分答案,但是不会check
这个时候就要换一下思想
三分套三分套三分坐标即可
复杂度(O(n(log_n)^3))
代码
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here
");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e2+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-5;
int n;
double x[maxn],y[maxn],z[maxn];
double check3(double a,double b,double c){
double ma=-1;
for(int i=1;i<=n;i++){
ma=max(ma,sqrt((a-x[i])*(a-x[i])+(b-y[i])*(b-y[i])+(c-z[i])*(c-z[i])));
}
return ma;
}
double check2(double x,double y){
double zl=-1e5,zr=1e5;
while(zl+eps<=zr){
double zmid1=(zr-zl)/3+zl,zmid2=2*(zr-zl)/3+zl;
if(check3(x,y,zmid1)<check3(x,y,zmid2)){
zr=zmid2;
}else{
zl=zmid1;
}
}
return check3(x,y,zl);
}
double check1(double x){
double yl=-1e5,yr=1e5;
while(yl+eps<=yr){
double ymid1=(yr-yl)/3+yl,ymid2=2*(yr-yl)/3+yl;
if(check2(x,ymid1)<check2(x,ymid2)){
yr=ymid2;
}else{
yl=ymid1;
}
}
return check2(x,yl);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
}
double xl=-1e5,xr=1e5;
while(xl+eps<=xr){
double xmid1=(xr-xl)/3+xl,xmid2=2*(xr-xl)/3+xl;
if(check1(xmid1)<check1(xmid2)){
xr=xmid2;
}else{
xl=xmid1;
}
}
printf("%.10f
",check1(xl));
return 0;
}