1.题目描述:点击打开链接
2.解题思路:本题是半平面交的一个典型应用。本题要求在一个凸多边形中找一点,使得这个点到凸多边形的最短距离最大。显然需要利用二分法来确定,那么如何来确定这个距离是否存在呢?可以想象,如果存在的话,那么把这个凸多边形的每条边都向内部平移这段距离后,交出来的区域至少包含一个点。只要这个区域是存在的,那么就说明这个距离是可行的。而这正是半平面交的典型应用。这样,二分答案,然后用半平面交来判断这个区域是否存在即可。总的时间复杂度为O(NlogN)。
3.代码:
#include<iostream>
#include<algorithm>
#include<cassert>
#include<string>
#include<sstream>
#include<set>
#include<bitset>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<complex>
#include<functional>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define me(s) memset(s,0,sizeof(s))
#define pb push_back
#define lid (id<<1)
#define rid (id<<1|1)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
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);}
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
double Length(Vector a){return sqrt(Dot(a,a));}
Vector Normal(Vector a){double L=Length(a);return Vector(-a.y/L,a.x/L);}
double PolygonArea(vector<Point>p)
{
int n=p.size();
double area=0;
for(int i=1;i<n-1;i++)
area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/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;
}
};
bool OnLeft(Line L,Point p)
{
return Cross(L.v,p-L.p)>0;
}
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;
}
const int INF=1e8;
const double eps=1e-6;
vector<Point> HalfplaneIntersection(vector<Line>L)
{
int n=L.size();
sort(L.begin(),L.end());
int first,last;
vector<Point> p(n);
vector<Line> q(n);
vector<Point> ans;
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 ans;
p[last]=GetLineIntersection(q[last],q[first]);
for(int i=first;i<=last;i++)ans.push_back(p[i]);
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
vector<Point>p,v,normal;
int m,x,y;
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
p.push_back(Point(x,y));
}
if(PolygonArea(p)<0)reverse(p.begin(),p.end());//此处是一个小技巧,如果面积为负,说明是顺时针输入的,需要翻转数组
for(int i=0;i<n;i++)
{
v.push_back(p[(i+1)%n]-p[i]);
normal.push_back(Normal(v[i]));
}
double LL=0,RR=20000;
while(RR-LL>1e-6)
{
vector<Line>L;
double mid=LL+(RR-LL)/2;
for(int i=0;i<n;i++)
L.push_back(Line(p[i]+normal[i]*mid,v[i]));
vector<Point>poly=HalfplaneIntersection(L);
if(poly.empty())RR=mid;
else LL=mid;
}
printf("%.6lf\n",LL);
}
}