题目链接
题目大意
有n个点,求出距离最大的两个点之间的平方距离
解题思路
先求出来凸包,在凸包上跑旋转卡壳,求出最大距离
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1e5+5;
struct node
{
double x,y;
node(){}
node(double X,double Y)
{
x=X;
y=Y;
}
node operator - (const node &a)
{
return node(x-a.x,y-a.y);
}
};
node e[N],ch[N];
int n,m;
int cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
double cross(node a,node b)
{
return a.x*b.y-b.x*a.y;
}
void Andrew()
{
sort(e,e+n,cmp);
m=0;
for(int i=0;i<n;i++)
{
while(m>1 && cross(ch[m-1]-ch[m-2],e[i]-ch[m-2])<=0.0)
m--;
ch[m++]=e[i];
}
int k=m;
for(int i=n-2;i>=0;i--)
{
while(m>k && cross(ch[m-1]-ch[m-2],e[i]-ch[m-2])<=0.0)
m--;
ch[m++]=e[i];
}
if(n>1)
m--;
}
double dis(node a,node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double cross1(node p1,node p2, node p0)
{
double x1=p1.x-p0.x;
double y1=p1.y-p0.y;
double x2=p2.x-p0.x;
double y2=p2.y-p0.y;
return(x1*y2-x2*y1);
}
double Diam()
{
if(m==1)
return dis(ch[0],ch[1]);
int j=2;
double ans=0;
for(int i=0;i<m;i++)
{
while(cross1(ch[i],ch[i+1],ch[j])<cross1(ch[i],ch[i+1],ch[j+1]))
j=(j+1)%m;
ans=max(ans,max(dis(ch[i],ch[j]),dis(ch[i+1],ch[j])));
}
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lf %lf",&e[i].x,&e[i].y);
Andrew();
printf("%d\n",(int)Diam());
}