UVALive 3890 Most Distant Point from the Sea (半平面交)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define FRER() freopen("in.txt","r",stdin)
#define FREW() freopen("out.txt","w",stdout)
#define go int T;cin>>T;for(int kase=0;kase<T;kase++)
#define debug cout<<"****************"<<endl
#define lowbit(x) x&(-x)
#define eps 1e-6
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int maxn = 300;
struct Point{
    double x,y;
    Point(double _x=0,double _y=0):x(_x),y(_y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double p){return Vector(A.x*p,A.y*p);    }
Vector operator / (Vector A,double p){return Vector(A.x/p,A.y/p);    }
double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}
double Length(Vector A){return sqrt(Dot(A,A));}
double Angle(Vector A,Vector B){return acos(Dot(A,B) / Length(A) / Length(B));}
double Cross(Vector A,Vector B){return A.x*B.y - A.y*B.x;}
double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}
Vector Rotate(Vector A,double rad){
	return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
Vector Normal(Vector A){
	double L = Length(A);
	return Vector(-A.y/L,A.x/L);
}
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){
	Vector u=P-Q;
	double t=Cross(w,u)/Cross(v,w);
	return P+v*t;
} 
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& rhs) const{
		return ang < rhs.ang;
	}
};
Point GetLineIntersection(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 OnLeft(Line L,Point P){
	return Cross(L.v,P-L.P) > 0;
}
int HalfplaneIntersection(Line* L,int n,Point* poly){
	sort(L,L+n);
	int first,last;
	Point* p = new Point[n];
	Line*  q = new Line[n] ;
	q[first = last = 0] = L[0];
	for(int i=1;i<n;i++){
		while(first < last && !OnLeft(L[i],p[last-1])) last --;
		while(first < last && !OnLeft(L[i],p[first]))  first++;
		q[++last] = L[i];
		if(fabs(Cross(q[last].v,q[last-1].v))<eps){
			last--;
			if(OnLeft(q[last],L[i].P)) q[last] = L[i];
		}
		if(first<last) p[last-1] = GetLineIntersection(q[last-1],q[last]);
	}
	while(first<last&&!OnLeft(q[first],p[last-1])) last--;
	if(last-first<=1) return 0;
	p[last] = GetLineIntersection(q[last],q[first]);
	int m = 0;
	for(int i=first;i<=last;i++) poly[m++] = p[i];
	return m;
}
Point p[maxn],poly[maxn];
Line L[maxn];
Vector v[maxn],v2[maxn];
int main(){
	//FRER();
	//FREW();
	int n;
	while(scanf("%d",&n)==1&&n){
		for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
		for(int i=0;i<n;i++){
			v[i] = p[(i+1)%n] - p[i];
			v2[i] = Normal(v[i]);
		}
		double left = 0 , right = 20000;
		while(right - left > eps){
			double mid = left + (right - left) / 2;
			for(int i=0;i<n;i++) L[i] = Line(p[i]+v2[i]*mid,v[i]);
			int m = HalfplaneIntersection(L,n,poly);
			if(!m) right = mid;
			else left = mid;
		}
		printf("%.6f\n",left);
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值