有一个性质,最远点对一定是凸包上的点的连线,直接求凸包,然后枚举下点距离即可。 这题输出的是距离的平方。><。。。 #include <stdio.h> #include <stdlib.h> #include <iostream> #include <algorithm> #include <math.h> using namespace std; const int MAX = 50010; const double eps = 1e-6; typedef struct Point{ double x,y; }Point; Point c[MAX]; bool dy(double x,double y) // x > y { return x > y + eps; } bool xy(double x,double y) // x < y { return x < y - eps; } bool dyd(double x,double y) // x >= y { return x > y - eps; } bool xyd(double x,double y) // x <= y { return x < y + eps; } bool dd(double x,double y) // x == y { return fabs( x - y ) < eps; } double crossProduct(Point a,Point b,Point c)//向量 ac 在 ab 的方向 { return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y); } double Distance(Point a,Point b) // a b 两点之间的距离 { return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) ); } double Dis(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( dd(a.y ,b.y ) ) return xy(a.x, b.x); return xy(a.y,b.y); } bool cmp1(Point a,Point b) // 第二次排序 { return xy( crossProduct(c[0],a,b), 0.0 ); } Point stk[MAX]; int top; double dis() { double ans = 0.0; for(int i=0; i<=top; i++) for(int k=0; k<=top; k++) if( i != k ) { double len = Dis(stk[i],stk[k]); if( dy( len,ans ) ) ans = len; } return ans; } double Graham(int n) { sort(c,c+n,cmp); sort(c+1,c+n,cmp1); top = 0; stk[top++] = c[0]; stk[top++] = c[1]; stk[top++] = c[2]; top--; for(int i=3; i<n; i++) { while(1) { Point a,b; a = stk[top]; b = stk[top-1]; if( xyd( crossProduct(a,b,c[i]), 0.0 ) ) top--; else break; } stk[++top] = c[i]; } return dis(); } int main() { int n; while( ~scanf("%d",&n) && n ) { for(int i=0; i<n; i++) scanf("%lf %lf",&c[i].x,&c[i].y); int ans = (int)Graham(n); printf("%d/n",ans); } return 0; }