经过一番挣扎,WNJXYK还是成功逃脱了,他来到了学校的后花园,后花园里有N个草堆 (2 <= N <= 50,000) ,每个草堆用(x,y)的坐标来表示(-10,000 <=x,y<= 10,000),当然两个草堆不可能在同一个坐标。WNJXYK决定躲在其中一个草堆里。
吸取了上题翻车的经验,WNJXYK决定先计算一下自己的躲法安不安全,于是他想知道这些草垛之间,距离最大的两个之间的距离是多少?
Input
第一行一个整数N
接下来N行,每行一对整数x,y,表示坐标
Output
一行,输出最远点对之间距离的平方
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
Hint
(0,0)到(1,1)距离的平方为2
思路:
求解出最大凸包,最长距离一定由凸包的两点构成
求解最长距离可以用凸包卡壳。
void RC(){ //旋转卡壳求距离
int ans=0,j=1;
sta[top]=sta[0];
for(int i=0;i<top;i++){
while(cross(p[sta[i]],p[sta[i+1]],p[sta[j]]) <cross(p[sta[i]],p[sta[i+1]],p[sta[j+1]]))
j=(j+1)%top;
ans=max(ans,max(dis(p[sta[i]],p[sta[j]]),dis(p[sta[i+1]],p[sta[j+1]])));
}
printf("%d\n",ans);
}
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N = 50005;
struct Point{
int x,y;
Point(){
}
Point(int xx,int yy){
x=xx;
y=yy;
}
}p[N];
Point operator-(const Point a,const Point b){
return Point(a.x-b.x,a.y-b.y);
}
int operator*(const Point a,const Point b){
return a.x*b.y-a.y*b.x;
}
int cross(Point a,Point b,Point c){
return (b-a)*(c-a);
}
int dis(Point a,Point b){
int X=(a.x-b.x)*(a.x-b.x);
int Y=(a.y-b.y)*(a.y-b.y);
return X+Y;
}
bool judge(Point a,Point b){
if(a.y==b.y) return a.x<b.x;
return a.y<b.y;
}
bool cmp(Point b,Point c){
Point a=p[0];
int ans=cross(a,b,c);
if(ans==0) dis(a,b)>dis(a,c);
return ans>0;
}
int n,top,sta[N];
void solve(){ //求解最大凸包
sta[0]=0;
sta[1]=1;
top=1;
for(int i=2;i<n;i++){
while(top&&cross(p[sta[top-1]],p[sta[top]],p[i])<=0) top--;
sta[++top]=i;
}
top++;
}
int main(){
while(~scanf("%d",&n)){
for(int i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
int t=0;
for(int i=1;i<n;i++) //找基点
if(judge(p[i],p[t])) t=i;
swap(p[0],p[t]);
sort(p+1,p+n,cmp); //极角排序
solve();
int ans=0;
for(int i=0;i<top;i++) //求最大距离
for(int j=i+1;j<top;j++)
ans=max(ans,dis(p[sta[i]],p[sta[j]]));
printf("%d\n",ans);
}
return 0;
}