# UVA1396 / POJ 3525 Most Distant Point from the Sea 题解 【半平面交】【二分答案】

## Description

The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: “Where is the most distant point from the sea?” The answer to this question for Honshu was found in 1996. The most distant point is located in former Usuda Town, Nagano Prefecture, whose distance from the sea is 114.86 km.

In this problem, you are asked to write a program which, given a map of an island, finds the most distant point from the sea in the island, and reports its distance from the sea. In order to simplify the problem, we only consider maps representable by convex polygons.

## Input

The input consists of multiple datasets. Each dataset represents a map of an island, which is a convex
polygon. The format of a dataset is as follows.

$n\\ x_1\qquad y_1\\ \qquad \vdots\\ x_n\qquad y_n$

Every input item in a dataset is a non-negative integer. Two input items in a line are separated by a space.

$n$ in the first line is the number of vertices of the polygon, satisfying $3 ≤ n ≤ 100$. Subsequent $laetx n$ lines are the $x$- and $y$-coordinates of the $n$ vertices. Line segments $(x_i , y_i) – (x_{i+1}, y_{i+1}) (1 ≤ i ≤ n − 1)$ and the line segment $(x_n, y_n) – (x_1, y_1)$ form the border of the polygon in counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. All coordinate values are between $0$ and $10000$, inclusive.

You can assume that the polygon is simple, that is, its border never crosses or touches itself. As
stated above, the given polygon is always a convex one.

The last dataset is followed by a line containing a single zero.

## Output

For each dataset in the input, one line containing the distance of the most distant point from the sea
should be output. An output line should not contain extra characters such as spaces.

The answer should not have an error greater than $0.00001 (10^{-5})$. You may output any number of
digits after the decimal point, provided that the above accuracy condition is satisfied.

## Sample Input

4
0 0
10000 0
10000 10000
0 10000
3
0 0
10000 0
7000 1000
6
0 40
100 20
250 40
250 70
100 90
0 70
3
0 0
10000 10000
5000 5001
0


## Sample Output

5000.000000
494.233641
34.542948
0.353553


## Code：

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define db long double
#define eps 1e-10
struct pnt
{
db x,y;
pnt(db x,db y)
{
this->x=x;
this->y=y;
}
pnt(){}
friend pnt operator +(pnt a,pnt b)
{return pnt(a.x+b.x,a.y+b.y);}
friend pnt operator -(pnt a,pnt b)
{return pnt(a.x-b.x,a.y-b.y);}
friend db operator *(pnt a,pnt b)
{return a.x*b.y-a.y*b.x;}
pnt times(db k)
{return pnt(x*k,y*k);}
db len()
{return sqrt(x*x+y*y);}
}p[105],t[105];
struct seg
{
pnt a,b;
db d;
seg(pnt a,pnt b)
{
this->a=a;
this->b=b;
d=atan2((b-a).y,(b-a).x);
}
seg(){}
friend bool operator <(seg x,seg y)
{
if(fabs(x.d-y.d)>eps)
return x.d<y.d;
return (y.a-x.a)*(y.b-x.a)>eps;
}
friend bool operator !=(seg x,seg y)
{return fabs(x.d-y.d)>eps;}
}s[105],f[105],q[105];
pnt its(seg x,seg y)
{
db u=(y.a-x.a)*(y.b-x.a);
db d=u+(y.b-x.b)*(y.a-x.b);
return x.a+(x.b-x.a).times(u/d);
}
int n;
bool check(db k)
{
for(int i=1;i<=n;++i)
{
db R=(s[i].b-s[i].a).len();
pnt v(-(s[i].b-s[i].a).y,(s[i].b-s[i].a).x);
v=v.times(k/R);
f[i]=seg(s[i].a+v,s[i].b+v);
}
std::sort(f+1,f+1+n);
int l=0,r=0;
for(int i=1;i<=n;++i)
if(f[i]!=f[i-1])
{
while(r-l>1&&(f[i].b-t[r])*(f[i].a-t[r])>-eps)
--r;
while(r-l>1&&(f[i].b-t[l+2])*(f[i].a-t[l+2])>-eps)
++l;
q[++r]=f[i];
if(r-l>1)
t[r]=its(q[r],q[r-1]);
}
while(r-l>1&&(q[l+1].b-t[r])*(q[l+1].a-t[r])>eps)
--r;
t[l+1]=its(q[l+1],q[r]);
db sum=t[r]*t[l+1];
for(int i=l+1;i<r;++i)
sum+=t[i]*t[i+1];
return sum>eps;
}
int main()
{
scanf("%d",&n);
f[0].d=233;
while(n)
{
for(int i=1;i<=n;++i)
{
scanf("%Lf%Lf",&p[i].x,&p[i].y);
if(i>1)
s[i]=seg(p[i-1],p[i]);
}
1[s]=seg(p[n],p[1]);
db l=0,r=5000,mid;
while(r-l>1e-9)
{
mid=(l+r)/2;
if(check(mid))
l=mid;
else
r=mid;
}
printf("%.8Lf\n",l);
scanf("%d",&n);
}
return 0;
}


Subscribe

/* */