2018-2019 ACM-ICPC, Asia Nanjing-D: Country Meow(三分)

CountryMeow防御策略
在24世纪的宇宙中,CountryMeow面临来自CountryWoof的威胁,总统决定建立新的作战指挥中心,以确保各城市间能有效沟通。任务是找到指挥中心的最佳位置,使得其与所有城市的最大欧几里得距离最小。通过三重三分法求解三维空间中的最优坐标。

Problem D. Country Meow
Input file: standard input
Output file: standard output

In the 24th century, there is a country somewhere in the universe, namely Country Meow. Due to advanced technology, people can easily travel in the 3-dimensional space.
There are N cities in Country Meow. The i-th city is located at (xi,yi,zi)(x_i, y_i, z_i)(xi,yi,zi) in Cartesian coordinate.
Due to the increasing threat from Country Woof, the president decided to build a new combatant command, so that troops in different cities can easily communicate. Hence, the Euclidean distance between the combatant command and any city should be minimized.
Your task is to calculate the minimum Euclidean distance between the combatant command and the farthest city.
Input
The first line contains an integer N(1≤N≤100)N (1 ≤ N ≤ 100)N(1N100).
The following N lines describe the i-th city located.Each line contains three integers xi,yi,zi(−100000≤xi,yi,zi≤100000)x_i, y_i, z_i(−100000 ≤ x_i, y_i, z_i ≤ 100000)xi,yi,zi(100000xi,yi,zi100000).
Output
Print a real number — the minimum Euclidean distance between the combatant command and the farthest city. Your answer is considered correct if its absolute or relative error does not exceed 10−310^{−3}103. Formally, let your answer be a, and the jury’s answer be b. Your answer is considered correct if ∣a−b∣max(1,∣b∣)≤10−3\frac {|a−b|}{max(1,|b|)} ≤ 10^{−3}max(1,b)ab103.
Examples
3
0 0 0
3 0 0
0 4 0
2.500000590252103
4
0 0 0
1 0 0
0 1 0
0 0 1
0.816496631812619

思路:三分x套三分y套三分z。

#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+10;
typedef long long ll;
struct Point{double x,y,z;}p[MAX];
double dis(Point A,Point B){return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)+(A.z-B.z)*(A.z-B.z));}
int n;
double cal(double x,double y,double z)
{
    double sum=0;
    for(int i=0;i<n;i++)sum=max(sum,dis(p[i],(Point){x,y,z}));
    return sum;
}
double cal(double x,double y)
{
    double l=-100000,r=100000,ans=1e18;
    for(int i=1;i<=50;i++)
    {
        double mid=(l+r)/2;
        double m=(mid+r)/2;
        double L=cal(x,y,mid);
        double R=cal(x,y,m);
        if(L>R)l=mid;
        else r=m;
        ans=min(ans,L);
        ans=min(ans,R);
    }
    return ans;
}
double cal(double x)
{
    double l=-100000,r=100000,ans=1e18;
    for(int i=1;i<=50;i++)
    {
        double mid=(l+r)/2;
        double m=(mid+r)/2;
        double L=cal(x,mid);
        double R=cal(x,m);
        if(L>R)l=mid;
        else r=m;
        ans=min(ans,L);
        ans=min(ans,R);
    }
    return ans;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z);
    double l=-100000,r=100000,ans=1e18;
    for(int i=1;i<=50;i++)
    {
        double mid=(l+r)/2;
        double m=(mid+r)/2;
        double L=cal(mid);
        double R=cal(m);
        if(L>R)l=mid;
        else r=m;
        ans=min(ans,L);
        ans=min(ans,R);
    }
    printf("%.15f\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值