Problem 3. island
Input file: island.in
Output file: island.out
Time limit: 1 second
Mr.H到了某个小岛,这个小岛的海岸线组成了一个凸多边形,Mr.H觉得离海水最远的地方最舒适,
所以他希望你帮忙求出,他最远能离海水多远?(离海水的距离就是离最近的海水的距离)。
Input
第1 行包含1 个整数n,表示多边形的点数。
接下来n 行,每行2 个数,表示一个点(按逆时针给出)。
Output
一个数,表示最远的距离(保留两位小数)。
Sample
island.in
40
0
1 0
1 1
0 1.0
island.out
0.5
Note
对于30% 的数据,1 <= n <= 10。
对于100% 的数据,1 <= n <= 100,所有坐标绝对值不超过1000。
思路:
ans满足二分的性质,二分答案mid,考虑怎么check。
半平面交,按照逆时针给出的凸包,可以用若干直线左半部分围成。
按照给出点顺序记录直线,合法区间就在半平面交中。
把每条直线向凸包内部(逆时针90度方向)平移mid距离,如果还有合法的半平面交的区域,就一定有满足答案为mid的点。
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#define MAXN 1000
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define sign(x) ((x > -eps) - (x < eps))
using namespace std;
const int N = 1010;
struct point {
double x, y;
point(){}
point(double x, double y):x(x),y(y){}
double ang() const {
return atan2(y, x);
}
double len() const {
return sqrt(x * x + y * y);
}
};
bool operator<(const point &r, const point &s) {
return r.x < s.x || (r.x == s.x && r.y < s.y);
}
point operator+(const point &r, const point &s) {
return point(r.x + s.x, r.y + s.y);
}
point operator-(const point &r, const point &s) {
return point(r.x - s.x, r.y - s.y);
}
point operator*(const point &r, double s) {
return point(r.x * s, r.y *s);
}
point operator/(const point &r, double s) {
return point(r.x / s, r.y / s);
}
double cross(const point &r, const point &s) {
return r.x * s.y - r.y * s.x;
}
double dot(const point &r, const point &s) {
return r.x * s.x + r.y * s.y;
}
point rotate90(const point &u) {//逆时针旋转90
return point(-u.y, u.x);
}
point movedis(const point &u, double s) {//平移距离
return u * (s / u.len());
}
struct line {
point a, b;
point angl;//方向向量
double ang;
};
bool operator<(const line &r, const line &s) {
return r.ang < s.ang;
}
bool operator ==(const line &r, const line &s) {
return sign(r.ang - s.ang) == 0;
}
int n;
point p[105];
//判断该半平面包含点(不在边界上)
bool onleft(const line &l, const point &p) {
return cross(l.angl, p-l.a) > 0;
}
//判两直线平行
int parallel(line u,line v){
return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y));
}
//计算两直线交点,注意事先判断直线是否平行!
//线段交点请另外判线段相交(同时还是要判断是否平行!)
point intersection(line u, line v){
point ret=u.a;
double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))
/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
ret.x+=(u.b.x-u.a.x)*t;
ret.y+=(u.b.y-u.a.y)*t;
return ret;
}
int half_plane_intersect(line *h) {//半平面交
sort(h, h+n);
int first, last;
point *p = new point[n];
line *q = new line[n];
q[first = last = 0] = h[0];
for(int i = 1; i < n; i++) {
while(first < last && !onleft(h[i], p[last-1])) last--;
while(first < last && !onleft(h[i], p[first])) first++;
q[++last] = h[i];
if(parallel(q[last], q[last-1])) {
last--;
if(onleft(q[last], h[i].a)) q[last] = h[i];
}
if(first < last) p[last-1] = intersection(q[last-1], q[last]);
}
while(first < last && !onleft(q[first],p[last-1])) last--;
if(last - first <= 1) return 0;
p[last] = intersection(q[last], q[first]);
//delete p, q; 只需要判断存在时用
return last - first + 1;
}
bool check(double mid) {//是否有满足ans为mid的点
line h[105];
for(int i = 0; i < n; i++) {
point a = p[i];
point b = p[(i+1)%n];
point u = movedis(rotate90(b - a), mid);
h[i].a = a + u; h[i].b = b + u;
h[i].angl = b - a; h[i].ang = u.ang();
}
return half_plane_intersect(h);
}
int main() {
freopen("island.in","r",stdin);
freopen("island.out","w",stdout);
while(scanf("%d", &n) == 1 && n) {
for(int i = 0; i < n; i++) {
double x, y;
scanf("%lf%lf", &x, &y);
p[i].x = x; p[i].y = y;
}
double lf = 0.0, rg = 20000.0;
for(int i = 0; i < 100; i++) {
double mid = (lf + rg) / 2.0;
if(check(mid)) lf = mid;
else rg = mid;
}
printf("%.2f\n", (lf + rg) / 2.0);
}
return 0;
}