Gym - 101981D Country Meow(三分套三分套三分)

题目大意:

空间内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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值