http://poj.org/problem?id=2187
这里很明显不能直接暴力(50000*50000)=。=
我们要知道最远的两点一定是凸包上的顶点,所以首先构造凸包,然后枚举找出最远距离,注意这里是平方!还要注意n==2的情况,直接求距离
#include<iostream> #include<vector> #include<map> #include<stack> #include<algorithm> #include<queue> #include<list> #include<set> #include<string.h> #include<stdlib.h> #include<math.h> #include<stdio.h> #include<ctype.h> #include<iomanip> using namespace std; #define LL long long #define pi acos(-1) #define N 50010 #define INF 9999999999 #define eps 1e-8 int n; int top; struct point { int x,y; }p[N],stck[N],p0; int cross(point a,point b,point c) { return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); } int 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) { int t=cross(p0,a,b); return t>0 || (t==0 && dis(p0,a)<dis(p0,b)); } void tubao() { int i,j,k; k=0; for(i=1;i<n;i++) if(p[i].y<p[k].y || (p[i].y==p[k].y&&p[i].x<p[k].x)) k=i; p0=p[k]; p[k]=p[0]; p[0]=p0; sort(p+1,p+n,cmp); top=1; stck[0]=p[0]; stck[1]=p[1]; for(i=2;i<n;i++) { while(top>0&&cross(stck[top-1],stck[top],p[i])<0) top--; stck[++top]=p[i]; } top+=1; return ; } void solve() { int max=0; int s; int i,j; for(i=0;i<top;i++) for(j=i+1;j<top;j++) { s=dis(stck[i],stck[j]); if(s>max) max=s; } printf("%d\n",max); } int main() { // freopen("a.txt","r",stdin); while(scanf("%d",&n)!=EOF) { int i,j,k; for(i=0;i<n;i++) scanf("%d %d",&p[i].x,&p[i].y); if(n<3) { printf("%d\n",dis(p[0],p[1])); continue; } tubao(); solve(); } return 0; }