牛牛战队的比赛地(三分)

牛牛战队的比赛地(三分)

链接:https://ac.nowcoder.com/acm/contest/3006/B
来源:牛客网

题目描述

由于牛牛战队经常要外出比赛,因此在全国各地建立了很多训练基地,每一个基地都有一个坐标(x,y)
这周末,牛牛队又要出去比赛了,各个比赛的赛点都在x轴上。牛牛战队为了方便比赛,想找一个到达训练基地最大距离最小的地方作为比赛地。
这个问题对于牛牛战队太简单了,它就交给了你,你来帮他算一下~

输入描述:

输入数据第一行包含一个整数N(1N100000),表示牛牛战队训练基地的数量。

接下来N行,每行包括2个整数x,y(−10000≤x,y≤10000),表示每一个训练基地的坐标。

输出描述:

输出一个小数,表示选择的比赛地距离各训练基地最大距离的最小值。

如果你的答案是a,标准答案是b,当∣a−b∣ / max(1,∣b∣)≤10−4时,你的答案将被判定为正确。

输入

3
0 0
2 0
0 2

输出

2

说明

当在(0,0)比赛时,到三个训练基地的最大距离是2。可以证明这是最小值。

 

题目要求最大距离最小,很容易想到使用二分或者三分的方法去做。

这题使用三分会比较简单,二分会比较麻烦。

 

三分讲解:https://blog.csdn.net/weixin_43914593/article/details/103250854

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const double eps =1e-8;
16 const int mod=1e9+7;
17 const int maxn=1e5+10;
18 using namespace std;
19 
20 struct node
21 {
22     int x,y;
23 }PT[100005];
24 int n;
25 
26 double check(double x)//求到每个点距离的最大值 
27 {
28     double MAX=0;
29     for(int i=1;i<=n;i++)
30     {
31         double t=sqrt(PT[i].y*PT[i].y+(PT[i].x-x)*(PT[i].x-x));
32         MAX=max(MAX,t);
33     }
34     return MAX;
35 }
36 
37 int main()
38 {
39     #ifdef DEBUG
40     freopen("sample.txt","r",stdin);
41     #endif
42     
43     scanf("%d",&n);
44     for(int i=1;i<=n;i++)
45         scanf("%d %d",&PT[i].x,&PT[i].y);
46     double L=-10000,R=10000;
47     double mid1,mid2;
48     while(R-L>eps)    //单谷函数三分 
49     {
50         double t=(R-L)/3.0;
51         mid1=L+t;
52         mid2=R-t;
53         if(check(mid1)>check(mid2)) L=mid1;//极值点在mid1右侧 
54         else R=mid2; //极值点在mid2左侧
55     }
56     printf("%.4f\n",check(mid1));
57     
58     return 0;
59 }

 

 

 

 

-

posted @ 2020-02-15 01:47  jiamian22  阅读(324)  评论(0编辑  收藏  举报