最远点对裸题。
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int MAXN=5e4+100;
struct Point
{
int x,y;
Point(){}
Point(int _x,int _y)
{
x = _x;y = _y;
}
//向量
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
int operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
int operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
Point list[MAXN];
int stack[MAXN],top;
int cross(Point p0,Point p1,Point p2) //计算叉积 p0p1 X p0p2
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(Point p1,Point p2) //计算 p1p2的 距离
{
return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
int dis2(Point p1,Point p2)
{
return (p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y);
}
bool cmp(Point p1,Point p2) //极角排序函数 , 角度相同则距离小的在前面
{
int tmp=cross(list[0],p1,p2);
if(tmp>0) return true;
else if(tmp==0&&dis(list[0],p1)<dis(list[0],p2)) return true;
else return false;
}
void init(int n) //输入,并把 最左下方的点放在 list[0] 。并且进行极角排序
{
int i,k;
Point p0;
scanf("%d%d",&list[0].x,&list[0].y);
p0.x=list[0].x;
p0.y=list[0].y;
k=0;
for(i=1;i<n;i++)
{
scanf("%d%d",&list[i].x,&list[i].y);
if( (p0.y>list[i].y) || ((p0.y==list[i].y)&&(p0.x>list[i].x)) )
{
p0.x=list[i].x;
p0.y=list[i].y;
k=i;
}
}
list[k]=list[0];
list[0]=p0;
sort(list+1,list+n,cmp);
}
void graham(int n)
{
int i;
if(n==1) {top=0;stack[0]=0;}
if(n==2)
{
top=1;
stack[0]=0;
stack[1]=1;
}
if(n>2)
{
for(i=0;i<=1;i++) stack[i]=i;
top=1;
for(i=2;i<n;i++)
{
while(top>0&&cross(list[stack[top-1]],list[stack[top]],list[i])<=0) top--;
top++;
stack[top]=i;
}
}
}
int main()
{
int n;
while (~scanf("%d", &n)) {
init(n);
graham(n);
int ans = 0;
int j = 1;
top++;
for (int i = 0; i < top; i++) {
int p = stack[i], p1 = stack[(i+1)%top];
while (cross(list[p1], list[stack[(j+1)%top]], list[p]) >cross(list[p1], list[stack[j]], list[p]))
j = (j+1) % top;
ans = max(ans, max(dis2(list[p], list[stack[j]]), dis2(list[p1], list[stack[(j+1)%top]])));
}
printf("%d\n", ans);
}
return 0;
}