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

作者: wjyyy 分类: 二分,半平面交,解题报告,计算几何 发布时间: 2019-02-20 19:38

点击量:854

半平面交好题。

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

题意:

有一个四面环海的小岛,求出岛上到海边距离最远的点,并输出这个距离,逆时针以一个凸多边形的形式给出小岛的形状。

多组数据。

题解:

本题求的是多边形内部任意一点到边的最小距离的最大值。

做这道题的时候只是觉得这个题有点像二分的模型,并且有合理的二分方式。上面那句话是我写题解的时候口胡出来的,由此可以知道,很多的二分题都会在体现在题意中,另见NOIP 2018 赛道修建 题解

因为点 $ P_i$ 是逆时针给出来的,所以半平面交的可行域是向量 $ \overrightarrow{P_iP_{i+1}}(i<n),\overrightarrow{P_nP_1}$(下同) 的左侧。

二分是直接二分答案,因此考虑把每个向量向左平移 $ mid$ 个单位。平移这一操作很好完成,首先把向量逆时针旋转 $ 90^\circ$ ,然后放缩到原来长度 $ k=|P_iP_{i+1}|$ 的 $ \frac{mid}k$ 倍,得到向量 $ \overrightarrow v$ 令 $ P_i$ 和 $ P_{i+1}$ 同时加上 $ \overrightarrow v$ 即可。然后再做半平面交,bool check()函数的返回值为“面积大于 $ 0$ 为真”。

最后需要用long double输出。(不知道为什么double被卡了……)

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;
}

说点什么

avatar
  Subscribe  
提醒
/* */