#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 50005;
struct point
{
int x,y;
bool operator<(const point a)const
{
return y < a.y ||(y == a.y && x < a.x);
}
} PointSet[maxn],ch[maxn];
int multiply(point s,point e,point o)
{
return ((s.x-o.x)*(e.y-o.y)-(s.y-o.y)*(e.x-o.x));
}
int dist2(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(point a,point b)
{
if(multiply(a,b,PointSet[0]) == 0)
return dist2(a,PointSet[0]) < dist2(PointSet[0],b);
return multiply(a,b,PointSet[0]) > 0;
}
int rotating_calipers(point *ch,int n)
{
int q=1,ans=0;
ch[n]=ch[0];
for(int p=0; p<n; p++)
{
while(multiply(ch[p+1],ch[q+1],ch[p]) > multiply(ch[p+1],ch[q],ch[p]))
q=(q+1)%n;
ans=max(ans,max(dist2(ch[p],ch[q]),dist2(ch[p+1],ch[q+1])));
}
return ans;
}
void convex_hull(point *p,point *ch,int n,int &len)
{
sort(p, p+n);
ch[0]=p[0];
ch[1]=p[1];
int top=1;
for(int i=2; i<n; i++)
{
while(top>0 && multiply(ch[top],p[i],ch[top-1])<=0)
top--;
ch[++top]=p[i];
}
int tmp=top;
for(int i=n-2; i>=0; i--)
{
while(top>tmp && multiply(ch[top],p[i],ch[top-1])<=0)
top--;
ch[++top]=p[i];
}
len=top;
}
int main()
{
int n,len;
while(scanf("%d",&n)!=EOF)
{
memset(ch,0,sizeof(ch));
for(int i=0; i<n; i++)
scanf("%d%d",&PointSet[i].x,&PointSet[i].y);
convex_hull(PointSet,ch,n,len);
printf("%d\n",rotating_calipers(ch,len));
}
return 0;
}
旋卡求凸包直径
最新推荐文章于 2017-07-30 22:42:00 发布