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

题目地址: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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值