————ACM相关————|CodeForces - 1059D Nature Reserve (二分搜索)

Nature Reserve
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There is a forest that we model as a plane and live nn rare animals. Animal number ii has its lair in the point (xi,yi)(xi,yi). In order to protect them, a decision to build a nature reserve has been made.
The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river.
For convenience, scientists have made a transformation of coordinates so that the river is defined by y=0y=0. Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve.
Input
The first line contains one integer nn (1≤n≤1051≤n≤105) — the number of animals.
Each of the next nn lines contains two integers xixi, yiyi (?107≤xi,yi≤107?107≤xi,yi≤107) — the coordinates of the ii-th animal's lair. It is guaranteed that yi≠0yi≠0. No two lairs coincide.
Output
If the reserve cannot be built, print ?1?1. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed 10?610?6.
Formally, let your answer be aa, and the jury's answer be bb. Your answer is considered correct if |a?b|max(1,|b|)≤10?6|a?b|max(1,|b|)≤10?6.
Examples
input
Copy

1 0 1

output
Copy
0.5

input
Copy
3 0 1 0 2 0 -3

output
Copy
-1

input
Copy
2 0 1 1 1

output
Copy
0.625

Note
In the first sample it is optimal to build the reserve with the radius equal to 0.50.5 and the center in (0, 0.5)(0, 0.5).
In the second sample it is impossible to build a reserve.
In the third sample it is optimal to build the reserve with the radius equal to 5858 and the center in (12, 58)(12, 58).

题意:一个圆与X轴相切,问最小的半径,使得圆包含所有给定的点。

【————ACM相关————|CodeForces - 1059D Nature Reserve (二分搜索)】解题思路:求最小,很容易想到二分。我们二分半径,然后由于固定了与X轴相切,我们对于每一个点,就可以算出这个点在圆上的时候的圆心的极限距离。然后我们就可以求得每一个点的圆心的区间范围。然后所有点的区间范围都相交,那么证明这个半径可行,否则不可行。

#include using namespace std; double x[100010],y[100010]; int N; bool check(long double k) { long double l=-100000000000000000.0,r=100000000000000000.0,t; for(int i=1; i<=N; i++) { if(y[i]>k*2) return 0; t=sqrt(k*k-(k-y[i])*(k-(y[i]))); //计算圆心最远的位置 if(l < x[i]-t) l = x[i]-t; if(r > x[i]+t) r = x[i]+t; } return l0?y[i]:-y[i]; //必须都在一边long double l=0,r=100000000000000000.0,m; for(int i=1; i<=100; i++)//二分半径足够多的次数 { m=(l+r)/2.0; if(check(m)) r=m; else l=m; } printf("%.10Lf",m); return 0; }



    推荐阅读