题目大意:
空间内n个点求最小球覆盖。(n <= 100)
解题思路:
先三分x的坐标,再在x确定情况下三分y的坐标,再在x和y都确定的情况下三分z的坐标然后枚举每个点求距离。复杂度 O ( N ( l o g 3 2 N ) 3 ) O(N(log_{\frac{3}{2}}N)^3) O(N(log23N)3)。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 105;
double inf = 1e9;
double eps = 1e-8;
double x[maxn], y[maxn], z[maxn];
double lx, rx, ly, ry, lz, rz;
int n;
double get_d(double cx, double cy, double cz){
double ans = 0;
for(int i = 0; i < n; ++i){
double r = (cx-x[i])*(cx-x[i]) + (cy - y[i])*(cy - y[i]) + (cz - z[i])*(cz - z[i]);
ans = max(ans, r);
}return sqrt(ans);
}
double check_z(double cx, double cy){
double l = lz, r = rz;
double ans = get_d(cx, cy, l);
while(r-l > eps){
double d = (r-l)/3;
double lmid = l+d, rmid = r-d;
double lans = get_d(cx, cy, lmid), rans = get_d(cx,cy,rmid);
if(lans > rans) l = lmid, ans = rans;
else r = rmid, ans = lans;
}
return ans;
}
double check_y(double cx){
double l = ly, r = ry;
double ans = check_z(cx, l);
while(r - l > eps){
double d = (r-l)/3;
double lmid = l + d, rmid = r - d;
double lans = check_z(cx, lmid), rans = check_z(cx, rmid);
if(lans > rans) l = lmid, ans = rans;
else r = rmid, ans = lans;
}
return ans;
}
int main()
{
lx = ly = lz = inf;
rx = ry = rz = -inf;
cin>>n;
for(int i = 0; i < n; ++i){
cin>>x[i]>>y[i]>>z[i];
lx = min(lx, x[i]); rx = max(rx, x[i]);
ly = min(ly, y[i]); ry = max(ry, y[i]);
lz = min(lz, z[i]); rz = max(rz, z[i]);
}
double l = lx, r = rx;
double ans = check_y(l);
while(r-l > eps){
double d = (r-l)/3;
double lmid = l + d, rmid = r - d;
double lans = check_y(lmid), rans = check_y(rmid);
if(lans > rans) l = lmid, ans = rans;
else r = rmid, ans = lans;
}
printf("%.8f\n", ans);
}