POJ 3525 Most Distant Point from the Sea

时间:2021-08-09 13:27:26

http://poj.org/problem?id=3525

给出一个凸包,要求凸包内距离所有边的长度的最小值最大的是哪个

思路:二分答案,然后把凸包上的边移动这个距离,做半平面交看是否有解。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
const double finf=1e10;
const double eps=1e-;
const double Pi=acos(-);
int n,tot;
struct Point{
double x,y;
Point(){}
Point(double x0,double y0):x(x0),y(y0){}
}p[];
struct Line{
Point s,e;
double slop;
Line(){}
Line(Point s0,Point e0):s(s0),e(e0){}
}l[],L[],c[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-')f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
Point operator *(Point p,double x){
return Point(p.x*x,p.y*x);
}
Point operator /(Point p,double x){
return Point(p.x/x,p.y/x);
}
double operator *(Point p1,Point p2){
return p1.x*p2.y-p1.y*p2.x;
}
Point operator -(Point p1,Point p2){
return Point(p1.x-p2.x,p1.y-p2.y);
}
Point operator +(Point p1,Point p2){
return Point(p1.x+p2.x,p1.y+p2.y);
}
double sqr(double x){
return x*x;
}
double dis(Point p){
return sqrt(sqr(p.x)+sqr(p.y));
}
Point e(Point p){
double len=dis(p);
p=p/len;
return p;
}
Point turn(Point p,double x){
double Sin=sin(x),Cos=cos(x);
double X=Cos*p.x-Sin*p.y;
double Y=Cos*p.y+Sin*p.x;
return Point(X,Y);
}
bool cmp(Line p1,Line p2){
if (p1.slop!=p2.slop) return p1.slop<p2.slop;
else return (p1.e-p1.s)*(p2.e-p1.s)<=;
}
void build(double mid){
for (int i=;i<=tot;i++){
Point p=e(turn(l[i].e-l[i].s,Pi/2.0))*mid;
L[i].s=l[i].s+p;
L[i].e=l[i].e+p;
}
for (int i=;i<=tot;i++)
L[i].slop=l[i].slop;
std::sort(L+,L++tot,cmp);
}
Point inter(Line p1,Line p2){
double k1=(p2.e-p1.s)*(p1.e-p1.s);
double k2=(p1.e-p1.s)*(p2.s-p1.s);
double t=(k2/(k1+k2));
double x=p2.s.x+(p2.e.x-p2.s.x)*t;
double y=p2.s.y+(p2.e.y-p2.s.y)*t;
return Point(x,y);
}
bool jud(Line p1,Line p2,Line p3){
Point p=inter(p1,p2);
return (p-p3.s)*(p3.e-p3.s)>;
}
bool phi(){
int cnt=;
for (int i=;i<=tot;i++)
if (L[i].slop!=L[i-].slop) L[++cnt]=L[i];
int lll=,rrr=;c[lll]=L[];c[rrr]=L[];
for (int i=;i<=cnt;i++){
while (lll<rrr&&jud(c[rrr],c[rrr-],L[i])) rrr--;
while (lll<rrr&&jud(c[lll],c[lll+],L[i])) lll++;
c[++rrr]=L[i];
}
while (lll<rrr&&jud(c[rrr],c[rrr-],c[lll])) rrr--;
while (lll<rrr&&jud(c[lll],c[lll+],c[rrr])) lll++;
if (rrr-lll+>=) return ;
else return ;
}
bool check(double mid){
build(mid);
if (phi()) return ;
return ;
}
int main(){
while (scanf("%d",&n)!=EOF){
if (n==) return ;
for (int i=;i<=n;i++)
p[i].x=read(),p[i].y=read();
p[n+]=p[];
tot=;
for (int i=;i<=n;i++)
l[++tot]=Line(p[i],p[i+]);
for (int i=;i<=tot;i++) l[i].slop=atan2(l[i].e.y-l[i].s.y,l[i].e.x-l[i].s.x);
double ll=0.0,rr=finf;
while (rr-ll>eps){
double mid=(ll+rr)/2.0;
if (check(mid)) ll=mid;
else rr=mid;
}
printf("%.6f\n",ll);
}
}