2020牛客寒假算法基础集训营5 B.牛牛战队的比赛地 (二分/三分)

https://ac.nowcoder.com/acm/contest/3006/B

三分做法

 1 #include<bits/stdc++.h>
 2 #define inf 0x3f3f3f3f
 3 using namespace std;
 4 typedef long long ll;
 5 typedef pair<int,int> pll;
 6 const int maxn=1e5+10;
 7 const int mod =1e9+7;
 8 const double EPS = 1e-6;
 9 struct node
10 {
11     double x;
12     double y;
13 };
14 node s[maxn];
15 int n;
16 double Calc(double num)
17 {
18     double ans=0;
19     for(int i=1;i<=n;i++)
20     {
21         double q=sqrt((num-s[i].x)*(num-s[i].x)+s[i].y*s[i].y);
22         if(q>ans)
23         {
24             ans=q;
25         }
26     }
27     return ans;
28 }
29 int main()
30 {
31     cin >> n;
32     int a,b;
33     for(int i=1; i<=n; i++)
34     {
35         cin >> a >> b;
36         s[i].x=a;
37         s[i].y=b;
38     }
39     double Left, Right;
40     double mid, midmid;
41     double mid_value, midmid_value;
42     Left = -10000;
43     Right = 10000;
44     while (Left + EPS < Right)
45     {
46         mid = (Left + Right)/2;
47         midmid = (mid + Right)/2;
48         mid_value = Calc(mid);
49         midmid_value = Calc(midmid);
50         if (mid_value <= midmid_value)
51             Right = midmid;
52         else
53             Left = mid;
54     }
55     cout << Calc(Left) << endl;
56     return 0;
57 }

猜你喜欢

转载自www.cnblogs.com/AaronChang/p/12308543.html