您的位置:首页 > 其它

计算几何入门4--poj2187Beauty Contest

2016-01-09 17:04 267 查看
题目大意:求平面上的点的最大距离的平方

造出一个凸包,利用面积的上凸性(一条线段和凸包上其他点组成的三角形的面积),用两条直线卡住凸包求最大距离

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=50000+10;
int n,m,top=0;
struct point{
int x,y;
}p[maxn];
point sta[maxn*2];

int cal(point a,point b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

bool cmp(const point a,const point b){
return (a.x<b.x||(a.x==b.x&&a.y<b.y));
}

int crossx(point a,point b,point c){
return (c.y-a.y)*(b.x-a.x)-(b.y-a.y)*(c.x-a.x);
}

void graham(){
memset(sta,0,sizeof(sta));
top=0; sta[++top]=p[1]; sta[++top]=p[2];
for (int i=3;i<=n;++i){
while (top>1&&crossx(sta[top-1],sta[top],p[i])<=0) top--;
sta[++top]=p[i];
}
int tmp=top; sta[++top]=p[n-1];
for (int i=n-2;i>=1;--i){
while (top>tmp&&crossx(sta[top-1],sta[top],p[i])<=0) top--;
sta[++top]=p[i];
}
}

void xzqk(){
int pos=2; int ans=0;
for (int i=1;i<=top;++i){
while (crossx(sta[i],sta[i+1],sta[pos])<crossx(sta[i],sta[i+1],sta[pos+1])){
pos++; if (pos==top) pos=1;
}
ans=max(ans,max(cal(sta[i],sta[pos]),cal(sta[i+1],sta[pos+1])));
} printf("%d",ans);
}

int main(){
while (scanf("%d",&n)!=EOF){
for (int i=1;i<=n;++i)
scanf("%d%d",&p[i].x,&p[i].y);
if (n==2){
printf("%d\n",cal(p[1],p[2]));
continue;
}else if (n<2){
printf("%d\n",0);
continue;
}
sort(p+1,p+n+1,cmp);
graham(); xzqk();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: