当前位置: 代码迷 >> 综合 >> LA 3890 Most Distant Point from the Sea 半平面交 .
  详细解决方案

LA 3890 Most Distant Point from the Sea 半平面交 .

热度:107   发布时间:2023-09-23 03:56:54.0

题目地址:https://vjudge.net/problem/UVALive-3890

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b)  for(int i=a;i<=(int)(b);++i)
#define REPD(i,a,b) for(int i=a;i>=(int)(b);--i)
const double PI=acos(-1);
const double EPS=1e-8;
int dcmp(double x){if(fabs(x)<EPS) return 0;return x > 0 ? 1 : -1;
}
struct Point{double x,y;
};
typedef Point Vector;
bool operator < (const Point& p1, const Point& p2){ return p1.x<p2.x || (p1.x==p2.x && p1.y<p2.y) ;}
Vector operator / (const Point& A, double x){ return Vector{A.x/x, A.y/x};}
Vector operator * (const Vector& A, double x){ return Vector{A.x*x, A.y*x};}
Vector operator - (const Vector& A, const Vector& B){ return Vector{A.x-B.x,A.y-B.y};}
Vector operator + (const Point& A, const Vector& v){ return Point{A.x+v.x,A.y+v.y};}
Vector Rotate(const Point& p,double ang){ return Vector{p.x*cos(ang)-p.y*sin(ang),p.x*sin(ang)+p.y*cos(ang)};}
double Cross(const Vector& A, const Vector& B){	return A.x*B.y-A.y*B.x;}
double Dot(Vector A, Vector B){	return A.x*B.x+A.y*B.y;}
double Length(Vector v){ return sqrt(fabs(Dot(v,v)));}
Vector Unit(Vector v){ return v/Length(v);}
Vector Normal(Vector v) { return Rotate(Unit(v),PI/2); }struct Line
{Point p; Vector v;double ang;Line(){}Line(Point p, Vector v) : p(p),v(v) { ang=atan2(v.y,v.x); }bool operator < (const Line& l) const{return ang<l.ang;}
};
const int maxn=200+5;
Point poly[maxn];
Vector VT[maxn];
Line L[maxn],NL[maxn];bool OnLeft(const Point& p,const Line& L){return dcmp(Cross(L.v, p-L.p)) > 0;
}
Point GetInterPoint(Line a, Line b){Vector u=a.p-b.p;double t=Cross(b.v,u) / Cross(a.v,b.v);return a.p+a.v*t;
}
bool HalfplaneIntersecton(Line* L, int n){sort(L,L+n);int first,last;Point* p=new Point[n];Line* Q=new Line[n];Q[first=last=0]=L[0];REP(i,1,n-1){while(first<last && !OnLeft(p[last-1], L[i])) last--;while(first<last && !OnLeft(p[first], L[i])) first++;Q[++last]=L[i];if(dcmp(Cross(Q[last-1].v, Q[last].v))==0){last--;if(OnLeft(L[i].p, Q[last])) Q[last]=L[i];}if(first<last) p[last-1]=GetInterPoint(Q[last], Q[last-1]);}while(first<last && !OnLeft(p[last-1], Q[first])) last--;if(last-first<=1) return false;  //形成不了封闭的图形return true;
}
int main(int argc, char const *argv[])
{// freopen("input.in","r",stdin);int n;while(scanf("%d",&n)==1&&n){REP(i,0,n-1) scanf("%lf%lf",&poly[i].x,&poly[i].y);poly[n]=poly[0];REP(i,0,n-1) {L[i]=Line(poly[i], poly[i+1]-poly[i]);VT[i]=Normal(L[i].v);}double Left=0,Right=2e4;  while(dcmp(Right-Left)>0){double mid=(Left+Right)/2;REP(i,0,n-1) NL[i]=Line(L[i].p+VT[i]*mid,L[i].v);bool ok=HalfplaneIntersecton(NL,n);if(!ok) Right=mid;else Left=mid;}printf("%.6lf\n", Left);}return 0;
}


  相关解决方案