【二分+半平面交】 POJ 3525 Most Distant Point from the Sea

套上计算几何的模板。。。然后二分搞一下。。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>  
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 505
#define maxm 3000005
#define eps 1e-10
#define mod 998244353
#define INF 999999999
#define lowbit(x) (x&(-x))
#define mp mark_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R
//#pragma comment (linker,"/STACK:102400000,102400000")
typedef long long LL;
//typedef int LL;
using namespace std;
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
// head

struct Point
{
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) { }
};
typedef Point Vector;
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;
	}
};

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);
}
int dcmp(double x)
{
	if(fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}
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);
}

bool OnLeft(Line L, Point p)
{
	return Cross(L.v, p - L.P) > 0;
}

Point GetIntersection(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;
}
int HalfplaneInersection(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] = GetIntersection(q[last-1], q[last]);
	}
	while(first < last && !OnLeft(q[first], p[last-1])) last--;
	if(last - first <= 1) return 0;
	p[last] = GetIntersection(q[last], q[first]);
	int m = 0;
	for(int i = first; i <= last; i++) poly[m++] = p[i];
	return m;
}

int n;
Point p[maxn], poly[maxn];
Vector v[maxn], v2[maxn];
Line line[maxn];

void read(void)
{
	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]);
	}
}
bool check(double x)
{
	for(int i = 0; i < n; i++) line[i] = Line(p[i] + v2[i] * x, v[i]);
	int k = HalfplaneInersection(line, n, poly);
	if(k) return true;
	else return false;
}
void work(void)
{
	double top = 30000, bot = 0, mid;
	while(fabs(top - bot) > eps) {
		mid = (top+bot)/2;
		if(check(mid)) bot = mid;
		else top = mid;
	}
	printf("%.6f\n", mid);
}
int main(void)
{
	while(scanf("%d", &n), n != 0) {
		read();
		work();
	}
	return 0;
}


你可能感兴趣的:(HDU)