您的位置:首页 > 其它

Poj 2187 Beauty Contest_旋转凸包卡壳

2014-03-29 13:26 417 查看
题意:给你n个坐标,求最远的两点距离

思路:用凸包算法求处,各个定点,再用旋转凸包卡壳

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 50010
struct node{
int x,y,d;
}p
;
int dist(node a,node b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int crossproduct(node a,node b,node c){
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
bool cmp1(node a,node b){
return (a.x==b.x)?(a.y<b.y):(a.x<b.x);
}
bool cmp2(node a,node b){
int cp=crossproduct(p[0],a,b);
if(!cp)
return a.d<b.d;
return cp>0;
}
int Graham(int n){
int i;
sort(p,p+n,cmp1);
for(i=1;i<n;i++)
p[i].d=dist(p[0],p[i]);
sort(p+1,p+n,cmp2);
int top=1;
for(int i=2;i<n;i++){
while(top>0&&crossproduct(p[i],p[top-1],p[top])<=0)
--top;
p[++top]=p[i];
}
p[++top]=p[0];
int R=1,D=0;
for(int L=0;L<top;++L){
while(crossproduct(p[L],p[L+1],p[R])<crossproduct(p[L],p[L+1],p[R+1]))
R=(R+1)%top;
D=max(D,max(dist(p[L],p[R]),dist(p[L+1],p[R+1])));
}
return D;
}
int main(int argc, char** argv) {
int n;
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
printf("%d\n",Graham(n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: