http://poj.org/problem?id=3525
给出一个凸包,要求凸包内距离所有边的长度的最小值最大的是哪个
思路:二分答案,然后把凸包上的边移动这个距离,做半平面交看是否有解。
#include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<algorithm> const double finf=1e10; const double eps=1e-7; const double Pi=acos(-1); int n,tot; struct Point{ double x,y; Point(){} Point(double x0,double y0):x(x0),y(y0){} }p[200005]; struct Line{ Point s,e; double slop; Line(){} Line(Point s0,Point e0):s(s0),e(e0){} }l[200005],L[200005],c[200005]; int read(){ int t=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();} while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';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)<=0; } void build(double mid){ for (int i=1;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=1;i<=tot;i++) L[i].slop=l[i].slop; std::sort(L+1,L+1+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)>0; } bool phi(){ int cnt=1; for (int i=2;i<=tot;i++) if (L[i].slop!=L[i-1].slop) L[++cnt]=L[i]; int lll=1,rrr=2;c[lll]=L[1];c[rrr]=L[2]; for (int i=3;i<=cnt;i++){ while (lll<rrr&&jud(c[rrr],c[rrr-1],L[i])) rrr--; while (lll<rrr&&jud(c[lll],c[lll+1],L[i])) lll++; c[++rrr]=L[i]; } while (lll<rrr&&jud(c[rrr],c[rrr-1],c[lll])) rrr--; while (lll<rrr&&jud(c[lll],c[lll+1],c[rrr])) lll++; if (rrr-lll+1>=3) return 1; else return 0; } bool check(double mid){ build(mid); if (phi()) return 1; return 0; } int main(){ while (scanf("%d",&n)!=EOF){ if (n==0) return 0; for (int i=1;i<=n;i++) p[i].x=read(),p[i].y=read(); p[n+1]=p[1]; tot=0; for (int i=1;i<=n;i++) l[++tot]=Line(p[i],p[i+1]); for (int i=1;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 ",ll); } }