

#include<stdio.h> #include<string.h> #include<stdlib.h> struct node { long long x,y; }p[50003],a[50003],t1; long long mul(node p1,node p2,node p3) { return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x); } long long dis(node a,node b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } int cmp(const void*p1,const void*p2) { node *c=(node*)p1; node *d=(node*)p2; long long k=mul(p[0],*c,*d); if(k<0||(!k&&dis(*c,p[0])<dis(*d,p[0]))) return 1; return -1; } int n; int top; void tubao() { int i; top=2; a[0]=p[0]; a[1]=p[1]; a[2]=p[2]; for(i=3;i<n;i++) { while(top>1&&mul(a[top-1],a[top],p[i])<=0&&top) top--; a[++top]=p[i]; } } int main() { int i,j; while(scanf("%d",&n)!=EOF) { int g=0; for(i=0;i<n;i++) scanf("%lld%lld",&p[i].x,&p[i].y); for(i=1;i<n;i++) { if(p[i].y<p[g].y) g=i; else if(p[i].y==p[g].y&&p[i].x<p[g].x) g=i; } if(g) { t1=p[g]; p[g]=p[0]; p[0]=t1; } qsort(p+1,n-1,sizeof(p[0]),cmp); tubao(); long long res=0; for(i=0;i<top;i++) for(j=i+1;j<=top;j++) { long long tmp=dis(a[i],a[j]); if(tmp>res) res=tmp; } printf("%lld\n",res); } return 0; }
题意:求平面最远点对。
分析:可以用旋转卡壳,也可以用凸包枚举,因为最远点对一定出现在凸包上。