Problem D. Country Meow 題解(三分套三分套三分)
阿新 • • 發佈:2020-11-21
題目連結
題目大意
給你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\n"); 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\n",check1(xl)); return 0; }