Codeforces contest 1059 problem D Nature Reserve —— 圆包含所有点的最小半径

There is a forest that we model as a plane and live n rare animals. Animal number i has its lair in the point (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=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 n (1≤n≤105) — the number of animals.

Each of the next n lines contains two integers xi, yi (−107≤xi,yi≤107) — the coordinates of the i-th animal’s lair. It is guaranteed that yi≠0. No two lairs coincide.

Output
If the reserve cannot be built, print −1. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed 10−6.

Formally, let your answer be a, and the jury’s answer be b. Your answer is considered correct if |a−b|max(1,|b|)≤10−6.

Examples
inputCopy
1
0 1
outputCopy
0.5
inputCopy
3
0 1
0 2
0 -3
outputCopy
-1
inputCopy
2
0 1
1 1
outputCopy
0.625
Note
In the first sample it is optimal to build the reserve with the radius equal to 0.5 and the center in (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 5/8 and the center in (1/2, 5/8).

题意:

给你n个点,让你求一个最小的圆半径,使得这个圆能够包含所有点并且与y=0相切。

题解:

先排除y轴两边都有点的情况,然后二分半径,对每一个点都求一个mid能够到达的左界和右界,注意不能写成rr-(r-y)(r-y)的形式,这样精度损失太大会错。要展开以后写成(2*r-y)*y这样的形式。当左界的最大值大于右界的最小值时就说明mid太小了。

#include<bits/stdc++.h>
using namespace std;
#define eps 1e-8
const int N=1e5+5;
struct node
{
    double x,y;
}p[N];
int n;
int judge(double r)
{
    double low=-1e17,high=1e17;
    for(int i=1;i<=n;i++)
    {
        if(r*2-p[i].y<eps)
            return 0;
        double len=(2*r-p[i].y)*p[i].y;
        len=sqrt(len);
        low=max(low,p[i].x-len),high=min(high,p[i].x+len);
    }
    return high>=low;
}
int main()
{
    int pos=0,neg=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lf%lf",&p[i].x,&p[i].y);
        if(p[i].y>0)
            pos=1;
        else
            neg=1;
        p[i].y=fabs(p[i].y);
    }
    if(pos*neg)
        return 0*printf("-1\n");
    double low=0,high=1e17;
    double ans;
    while((high-low)/max(1.0,low)>eps)
    {
        double mid=(low+high)/2;
        if(judge(mid))
            ans=mid,high=mid;
        else
            low=mid;
    }
    printf("%.7f\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/84071754
今日推荐