poj 2187 凸包+平面上点之间最大距离
来源:互联网 发布:土豆客户端mac版 编辑:程序博客网 时间:2024/05/12 06:11
/* 这道题目是算一个平面内的一些点的之间最大的距离,暴力肯定会超时,最远距离,一定是凸包上的两个点的距离, 先用模板找到凸包线上的点,然后枚举任意两点之间的距离,求出最大的距离!*/#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn=50005;int n;double len[maxn];struct point{ double x; double y; double there;} p[maxn],chs[maxn];bool cmp1(point a,point b){ if(a.y==b.y)return a.x<b.x; return a.y<b.y;}double get_there(point a,point b){ return atan2((a.y-b.y),(a.x-b.x));}bool cmp2(point a,point b){ if(a.there==b.there) return a.x<b.x; return a.there<b.there;}double dist_point2(point a,point b){ return (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y);}bool line_point1(point a,point b,point c){ double m; m=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); if(m>0)return false; if(m<0)return true; if(m==0)return true; return false;}int main(){ while(scanf("%d",&n)!=EOF) { double len; double max=0; int i,j; for(i=0; i<n; i++) { scanf("%lf %lf",&p[i].x,&p[i].y); } sort(p,p+n,cmp1);//排序,找基点 for(i=0; i<n; i++) { p[i].there=get_there(p[i],p[0]);//算极角 } sort(p+1,p+n,cmp2);//极角排序 chs[0]=p[0];//GrahamScan算法 chs[1]=p[1]; chs[2]=p[2]; int top=2; for(i=3; i<n; i++) { while(top>=1&&line_point1(chs[top-1],chs[top],p[i])) { top--; } chs[++top]=p[i]; } top++; for(j=0; j<top; j++)//就是这里枚举最大的距离 { for(i=j+1; i<top; i++) { len=dist_point2(chs[j],chs[i]); if(len>max) max=len; } } printf("%.0lf\n",max); } return 0;}