B cattle off winter break five teams to race (three points)

Here Insert Picture Description
For this problem, we want to find the shortest distance from the race to the farthest training base, we can at any distance from a base camp in the farthest calculated using a function. Then we can not directly traverse, need to find a suitable location in the entire range of axes, then we take the rule of thirds, first set a very apex of the left and a big right vertex, the maximum distance compare left and right vertex vertex, every rule out a third of the wrong option, you can find the answer on time, we need to pay attention to this question can be a decimal coordinate all, pay attention to accuracy

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
int n;
double x[N],y[N];
double f(double mid) 
{
    double mi = 0;
    for(int i = 1;i<=n;i++)
    {
        mi=max(mi,sqrt(y[i]*y[i]+(x[i]-mid)*(x[i]-mid)));
    } 
    return mi;
 } 
int main()
{
    scanf("%d",&n);
    for(int i = 1;i<=n;i++)
    {
        scanf("%lf%lf",&x[i],&y[i]);
    }
    double l=-1e4,r=1e4;
    for(int i=1;i<70;i++)
    {
        double mid1=l+(r-l)/3;
        double mid2=r-(r-l)/3;
        if(f(mid1) > f(mid2)) l=mid1;
        else r= mid2;
    }
    printf("%.10f",f(l));
    return 0;
}
Published 48 original articles · won praise 17 · views 4465

Guess you like

Origin blog.csdn.net/weixin_45757507/article/details/104317580