您的位置:首页 > 其它

poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)

2014-07-30 21:52 573 查看
/*  poj 2187 Beauty Contest
凸包:寻找每两点之间距离的最大值
这个最大值一定是在凸包的边缘上的!

求凸包的算法: Andrew算法!
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct Point{
Point(){}
Point(int x, int y){
this->x=x;
this->y=y;
}
int x, y;

static int cross(Point a, Point b){
return a.x*b.y - a.y*b.x;
}

static int dist(Point a, Point b){
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}

Point operator -(Point tmp){
return Point(x-tmp.x, y-tmp.y);
}
};

bool cmp(Point a, Point b){
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}

Point p[50005];
int ch[50005];
int n;

int main(){
int i;
while(scanf("%d", &n)!=EOF){
for(i=0; i<n; ++i)
scanf("%d%d", &p[i].x, &p[i].y);
sort(p, p+n, cmp);
int m=0;
//求下凸包, 如果某一个点不在线段之内,向量的叉积必定是<=0;
for(i=0; i<n; ++i){
while(m>1 && Point::cross(p[ch[m-1]]-p[ch[m-2]], p[i]-p[ch[m-2]])<=0) m--;
ch[m++]=i;
}
//为啥求上凸包的时候,坐标的从n-2开始:因为n-1点一定是在下凸包中的(因为它的横坐标最大,必然是包含其他节点的)
int k=m;
for(i=n-2; i>=0; --i){
while(m>k && Point::cross(p[ch[m-1]]-p[ch[m-2]], p[i]-p[ch[m-2]])<=0) m--;
ch[m++]=i;
}
--m;
int maxD=-1, j, d;
for(i=0; i<m; ++i)
for(j=i+1; j<=m; ++j)
if(maxD < (d=Point::dist(p[ch[i]], p[ch[j]])))
maxD=d;
printf("%d\n", maxD);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: