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

版权声明:http://blog.csdn.net/Mitsuha_。 https://blog.csdn.net/Mitsuha_/article/details/84193360

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 ( x i , y i , z i ) (x_i, y_i, z_i) 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) .
The following N lines describe the i-th city located.Each line contains three integers x i , y i , z i ( 100000 x i , y i , z i 100000 ) x_i, y_i, z_i(−100000 ≤ x_i, y_i, z_i ≤ 100000) .
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 1 0 3 10^{−3} . Formally, let your answer be a, and the jury’s answer be b. Your answer is considered correct if a b m a x ( 1 , b ) 1 0 3 \frac {|a−b|}{max(1,|b|)} ≤ 10^{−3} .
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;
}

猜你喜欢

转载自blog.csdn.net/Mitsuha_/article/details/84193360
今日推荐