[凸包最大内接圆 二分 半平面交] POJ 3525 Most Distant Point from the Sea

来源:互联网 发布:明朝 西方 知乎 编辑:程序博客网 时间:2024/04/30 20:17

二分答案 然后半平面向内缩 判断是否有交

#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>using namespace std;typedef double ld;const double eps=1e-7;inline int sgn(ld a){  if (fabs(a)<eps) return 0; if (a<0) return -1; return 1;}const int N=105;const double PI=acos(-1.0);struct P{  ld x,y;  P(ld x=0,ld y=0):x(x),y(y) { }  ld dist(){ return sqrt(x*x+y*y); }  P rot(double A){ return P(x*cos(A)-y*sin(A),x*sin(A)+y*cos(A)); }  friend P operator + (P A,P B) { return P(A.x+B.x,A.y+B.y); }  friend P operator - (P A,P B) { return P(A.x-B.x,A.y-B.y); }  friend ld operator * (P A,P B) { return A.x*B.y-B.x*A.y; }  friend P operator * (P A,ld B) { return P(A.x*B,A.y*B); }  friend P operator / (P A,ld B) { return P(A.x/B,A.y/B); }}p[N]; int n;struct Tl{  P p1,p2; ld ang;  Tl() { }  Tl(P _p1,P _p2){ p1=_p1,p2=_p2; ang=atan2(p2.y-p1.y,p2.x-p1.x); }  friend bool operator < (const Tl &A,const Tl &B){    return !sgn(A.ang-B.ang)?((A.p1-B.p1)*(B.p2-B.p1)<0):sgn(A.ang-B.ang)<0;  }  friend P cross(const Tl &A,const Tl &B){    ld ta=(A.p1-B.p1)*(A.p2-B.p1),tb=(A.p2-B.p2)*(A.p1-B.p2);    return (B.p1*tb+B.p2*ta)/(ta+tb);  }}L[N]; int tot;inline bool onleft(Tl A,Tl B,Tl C){  P p=cross(A,B);  return sgn((p-C.p1)*(C.p2-C.p1))<0;}int Q[N],l,r;inline bool hpi(){  sort(L+1,L+tot+1); int p=1;  for (int i=2;i<=tot;i++) if (sgn(L[i].ang-L[i-1].ang)) L[++p]=L[i];  tot=p;  l=1; r=0; Q[++r]=1;  for (int i=2;i<=tot;i++){    while (l<r && !onleft(L[Q[r]],L[Q[r-1]],L[i])) r--;    while (l<r && !onleft(L[Q[l]],L[Q[l+1]],L[i])) l++;    Q[++r]=i;  }  while (l<r && !onleft(L[Q[r]],L[Q[r-1]],L[Q[l]])) r--;  while (l<r && !onleft(L[Q[l]],L[Q[l+1]],L[Q[r]])) l++;  return r-l>=2;}inline bool check(ld mid){  tot=0;  for (int i=1;i<n;i++) L[++tot]=Tl(p[i],p[i+1]);  L[++tot]=Tl(p[n],p[1]);  for (int i=1;i<=tot;i++){    P tmp=L[i].p2-L[i].p1;    tmp=tmp.rot(PI/2);    tmp=tmp/tmp.dist()*mid;    L[i].p1=L[i].p1+tmp;    L[i].p2=L[i].p2+tmp;  }  return hpi();}int main(){  double x,y;  freopen("t.in","r",stdin);  freopen("t.out","w",stdout);  while (1){    scanf("%d",&n); if (!n) break;    for (int i=1;i<=n;i++) scanf("%lf%lf",&x,&y),p[i].x=x,p[i].y=y;    ld L=0,R=50000,MID;    while (R-L>eps)      if (check(MID=(L+R)/2))    L=MID;      else    R=MID;    printf("%.6lf\n",(double)(L+R)/2);  }  return 0;}
0 0
原创粉丝点击