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

题目大意:给出一个岛的海岸线的轮廓,求这个岛上的所有点到海岸的最长距离是多少。

思路:求多边形内切圆的问题要用二分+半平面交解决。二分半径的长度,然后将所有的边向左侧移动这个二分的长度,然后利用半平面交来判断是否能够满足条件。如果满足条件就提高下界,否则减小上界。

我的移动的方法是这样的,首先每条边都要用点向量式来表示,就是边上任意一点和这条边的方向向量。这样做以后的操作会方便很多。然后将每个直线的向左的法向量求出来(比如l的向量是v(x,y),那么它向左侧的法向量是(-y,x)),然后将法向量化成单位向量,然后这个直线方向向量不变,直线上的点向法向量方向移动二分的长度。详见代码。

CODE:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 110
#define EPS 1e-10
#define DCMP(a) (fabs(a) < EPS)
using namespace std;

struct Point{
	double x,y;

	Point(double _ = .0,double __ = .0):x(_),y(__) {}
	Point operator +(const Point &a)const {
		return Point(x + a.x,y + a.y);
	}
	Point operator -(const Point &a)const {
		return Point(x - a.x,y - a.y);
	}
	Point operator *(double a)const {
		return Point(x * a,y * a);
	}
	void Read() {
		scanf("%lf%lf",&x,&y);
	}
}point[MAX],p[MAX];
struct Line{
	Point p,v;
	double alpha;

	Line(Point _,Point __):p(_),v(__) {
		alpha = atan2(v.y,v.x);
	}
	Line() {}
	bool operator <(const Line &a)const {
		return alpha < a.alpha;
	}
}line[MAX],q[MAX];

int points,lines;

inline double Cross(const Point &a,const Point &b)
{
	return a.x * b.y - a.y * b.x;
}

inline bool OnLeft(const Line &l,const Point &p)
{
	return Cross(l.v,p - l.p) >= 0;
}

inline double Calc(const Point &a,const Point &b)
{
	return sqrt((a.x - b.x) * (a.x - b.x) +
				(a.y - b.y) * (a.y - b.y));
}

inline Point GetIntersection(const Line &a,const Line &b)
{
	Point u = a.p - b.p;
	double temp = Cross(b.v,u) / Cross(a.v,b.v);
	return a.p + a.v * temp;
}

inline bool HalfPlaneIntersection()
{
	int front = 1,tail = 1;
	q[tail] = line[1];
	for(int i = 2;i <= lines; ++i) {
		while(front < tail && !OnLeft(line[i],p[tail - 1]))	--tail;
		while(front < tail && !OnLeft(line[i],p[front]))	++front;
		if(DCMP(Cross(line[i].v,q[tail].v)))
			q[tail] = OnLeft(line[i],q[tail].p) ? q[tail]:line[i];
		else	q[++tail] = line[i];
		if(front < tail)	p[tail - 1] = GetIntersection(q[tail],q[tail - 1]);
	}
	while(front < tail && !OnLeft(line[front],p[tail - 1]))	--tail;
	if(tail - front <= 1)	return false;
	return tail > front;
}

inline void MakeLine(const Point &a,const Point &b,double adjustment)
{
	Point p = a,v = b - a;
	double length = Calc(a,b);
	Point _v(-v.y / length,v.x / length);
	p = p + _v * adjustment;
	line[++lines] = Line(p,v);
}

inline bool Judge(double ans)
{
	lines = 0;
	for(int i = 1;i < points; ++i)
		MakeLine(point[i],point[i + 1],ans);
	MakeLine(point[points],point[1],ans);
	sort(line + 1,line + lines + 1);
	return HalfPlaneIntersection();
}

int main()
{
	while(scanf("%d",&points),points) {
		for(int i = 1;i <= points; ++i)
			point[i].Read();
		double l = .0,r = 10000.0,ans = .0;
		while(r - l > EPS) {
			double mid = (l + r) * 0.5;
			if(Judge(mid))	l = mid,ans = mid;
			else	r = mid;
		}
		printf("%.6lf\n",ans);
	}
	return 0;
}

时间: 2024-07-29 06:55:48

POJ 3525 Most Distant Point from the Sea 二分+半平面交的相关文章

poj 3525 Most Distant Point from the Sea 半平面交 + 二分

题目来源: http://poj.org/problem?id=3525 分析: 题意:给定一个凸多边形,求多边形中距离边界最远的点到边界的距离. 思路 : 每次将凸多边形每条边往里平移d,判断是否存在核:二分d即可. 多边形边上的点(x , y)往里平移d 后的 坐标: s , e  为向量的 起点和终点, len 为起点和终点的距离, h 为平移的距离 x' = x + dx y' = y + dy dx = ( s.y - e.y ) / len * h ,( 原理 是 利用 三角形的相似

Poj 3525 Most Distant Point from the Sea

地址:http://poj.org/problem?id=3525 题目: Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5167   Accepted: 2331   Special Judge Description The main land of Japan called Honshu is an island surrounded by the s

POJ 3525 二分+半平面交

Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3812   Accepted: 1779   Special Judge Description The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural t

POJ 3525 Most Distant Point from the Sea (半平面交向内推进+二分半径)

题目链接 题意 : 给你一个多边形,问你里边能够盛的下的最大的圆的半径是多少. 思路 :先二分半径r,半平面交向内推进r.模板题 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <math.h> 5 const double eps = 1e-10 ; 6 7 using namespace std ; 8 9 struct node 10 { 11 do

POJ 3130 How I Mathematician Wonder What You Are! 半平面交

和POJ3335一样,只不过这题是逆时针 #include <iostream> #include <cstdio> #include <cmath> #define eps 1e-18 using namespace std; const int MAXN = 105; double a, b, c; int n, cnt; struct Point { double x, y; }point[MAXN], p[MAXN], tp[MAXN]; void Get_eq

POJ 2451 Uyuw&#39;s Concert(半平面交nlgn)

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <vector> #include <

poj 3384 半平面交

Feng Shui Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5183   Accepted: 1548   Special Judge Description Feng shui is the ancient Chinese practice of placement and arrangement of space to achieve harmony with the environment. George h

POJ 1474 Video Surveillance 半平面交

和POJ 3130,POJ 3335一样.求多边形的核 #include <iostream> #include <cstdio> #include <cmath> #define eps 1e-18 using namespace std; const int MAXN = 105; double a, b, c; int n, cnt; struct Point { double x, y; }point[MAXN], p[MAXN], tp[MAXN]; void

How I Mathematician Wonder What You Are! POJ - 3130(半平面交,多边形内核)

How I Mathematician Wonder What You Are! POJ - 3130 题意:判断多边形是否有内核 思路:半平面交题,逆时针存入 1 // 2 // Created by HJYL on 2020/2/6. 3 // 4 #include<iostream> 5 #include<stdio.h> 6 #include<algorithm> 7 #include<math.h> 8 using namespace std; 9