题意:求所有点中最远距离,即凸包的最长直径
思路:求凸包,旋转卡壳求最长直径,枚举各个点,找对踵点,更新距离
代码:
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<map>
#include<cstdio>
#include<cstring>
#include<math.h>
#define sqr(x) ((x)*(x))
using namespace std;
const double eps=1e-8;
const double pi=acos(-1.0);
int dcmp(double x)
{
if(x<-eps) return -1;
else return x>eps;
}
struct cpoint
{
double x,y;
cpoint(){}
cpoint (double x,double y) : x(x),y(y) {}
cpoint operator + (const cpoint &u) const {
return cpoint(x + u.x, y + u.y);
}
cpoint operator -(const cpoint &u) const
{
return cpoint (x-u.x,y-u.y);
}
double operator * (const cpoint &u) const
{
return x*u.y-y*u.x;
}
double operator ^ (const cpoint &u) const
{
return x*u.x+y*u.y;
}
bool operator == (const cpoint &u) const
{
return dcmp(x-u.x)==0&&dcmp(y-u.y)==0;
}
};
double cross(cpoint o,cpoint p,cpoint q)
{
return (p-o)*(q-o);
}
double dis(cpoint p,cpoint q)
{
return sqrt(sqr(p.x-q.x)+sqr(q.y-p.y));
}
double dissqr(cpoint p,cpoint q)
{
return sqr(p.x-q.x)+sqr(q.y-p.y);
}
cpoint bp;
int PolarCmp(const cpoint &p1,const cpoint &p2)
{
int u=dcmp(cross(bp,p1,p2));
return u>0 || (u==0&&dcmp(dissqr(bp,p1)-dissqr(bp,p2))<0);
}
void graham(cpoint pin[],int n,cpoint ch[],int &m)
{
int i,j,k,u,v;
memcpy(ch,pin,n*sizeof(cpoint));
for(i=k=0;i<n;i++)
{
u=dcmp(ch[i].x-ch[k].x);
v=dcmp(ch[i].y-ch[k].y);
if(v<0||(v==0&&u<0))
k=i;
}
bp=ch[k];
sort(ch,ch+n,PolarCmp);
n=unique(ch,ch+n)-ch;
if(n<=1) { m=n;return ;}
if(dcmp(cross(ch[0],ch[1],ch[n-1]))==0)
{
m=2;ch[1]=ch[n-1];return ;
}
ch[n++]=ch[0];
for(i=1,j=2;j<n;++j)
{
while(i>0&&dcmp(cross(ch[i-1],ch[i],ch[j]))<=0) i--;
ch[++i]=ch[j];
}
m=i;
}
double rotating (cpoint cp[],int n)
{
int i=1;
double res=0.0;
cp[n]=cp[0];
for(int j=0;j<n;++j)
{
while (dcmp(fabs(cross(cp[i+1], cp[j], cp[j+1]))-
fabs(cross(cp[i], cp[j], cp[j+1])) ) > 0)
i = (i + 1) % n;
res = max(res, max(dissqr(cp[i], cp[j]),
dissqr(cp[i+1], cp[j+1])));
}
return res;
}
cpoint dp[50010],ch[50010];
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%lf %lf",&dp[i].x,&dp[i].y);
graham(dp,n,ch,m);
double ans=rotating(ch,m);
printf("%d\n",(int)(ans+0.5));
}
return 0;
}