POJ 3525: Most Distant Point from the Sea 半平面交 二分

Most Distant Point from the Sea
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5740 Accepted: 2549 Special Judge

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  
x1 y1
  
xn yn

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 n lines are the x- and y-coordinates of the n vertices. Line segments (xiyi)–(xi+1yi+1) (1 ≤ i ≤ n − 1) and the line segment (xnyn)–(x1y1) 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

Source


求多边形内离边界最远的点的距离

二分答案 半平面交判定

有了上一次的经验 1min调出



#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

typedef double db;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=110;
const db inf=5000,eps=1e-8;

struct point
{
	db x,y;
	
	friend point operator +(const point &a,const point &b)
	{return (point){a.x+b.x,a.y+b.y};}
	
	friend point operator -(const point &a,const point &b)
	{return (point){a.x-b.x,a.y-b.y};}
	
	friend point operator *(db x,const point &a)
	{return (point){a.x*x,a.y*x};}
	
	friend db dis(const point &a,const point &b)
	{return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
	
	friend db cross(const point &x,const point &y,const point &bas)
	{
		point a(x-bas),b(y-bas);
		return a.x*b.y-a.y*b.x;
	}
}p[N];

int scnt;
struct segment
{
	point X,Y;db angle;
	
	friend bool operator <(const segment &a,const segment &b)
	{
		if(abs(a.angle-b.angle)<eps) return cross(a.X,a.Y,b.X)>0;
		return a.angle<b.angle;
	}
	
	friend point get_node(const segment &a,const segment &b)
	{
		point res;db s[2];
		s[0]=cross(a.X,a.Y,b.X);
		s[1]=cross(a.Y,a.X,b.Y);
		res.x=(b.Y.x*s[0]+b.X.x*s[1])/(s[0]+s[1]);
		res.y=(b.Y.y*s[0]+b.X.y*s[1])/(s[0]+s[1]);
		return res;
	}
}seg[N];

inline void add_seg(const point &a,const point &b,db x)
{
	//proportion
	point A,B;
	db pro=x/dis(a,b);
	A.x=a.x-(b-a).y*pro;
	A.y=a.y+(b-a).x*pro;
	B=A+b-a;
	seg[++scnt].X=A,seg[scnt].Y=B;
	seg[scnt].angle=atan2(B.y-A.y,B.x-A.x);
}

bool check(int a,int b,int c)
{
	point tmp(get_node(seg[a],seg[b]));
	return cross(seg[c].X,seg[c].Y,tmp)<0;
}

int q[N];

int hpi()
{
	sort(seg+1,seg+1+scnt);
	register int i,tot(1),head(0),tail(2);
	for(i=2;i<=scnt;++i)
		if(seg[i].angle-seg[tot].angle>eps)
			seg[++tot]=seg[i];
	q[0]=1;q[1]=2;
	for(i=3;i<=tot;++i)
	{
		while(head<tail-1 && check(q[tail-1],q[tail-2],i)) tail--;
		while(head<tail-1 && check(q[head],q[head+1],i)) head++;
		q[tail++]=i;
	}
	while(head<tail-1 && check(q[tail-1],q[tail-2],q[head])) tail--;
	while(head<tail-1 && check(q[head],q[head+1],q[tail-1])) head++;
	return tail-head;
}

int n;

bool solve(db x)
{
	scnt=0;
	register int i;
	for(i=1;i<=n;++i)
		add_seg(p[i],p[i+1],x);
	return hpi()>2;
}

db bisection_method()
{
	db l(0),r(inf),mid;
	while(l<r-eps)
	{
		mid=(l+r)*0.5;
		solve(mid) ? l=mid : r=mid;
	}
	return r;
}

int main()
{
	register int i;
	while(1)
	{
		n=read();
		if(!n) break;
		for(i=1;i<=n;++i)
			p[i].x=read(),p[i].y=read();
		p[n+1]=p[1];
		printf("%.6f\n",bisection_method());
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值