poj 2187 凸包 最大点对距

题目大意就是最大点对距,最大点对距的两端点一定是凸包顶点,所以找出形成凸包所需要的最少顶点,然后遍历找出最大点对距。

对凸包算法的详细讲解:http://www.cnblogs.com/Booble/archive/2011/03/10/1980089.html

#include<iostream>
#include<cstdio>
using namespace std;
struct point
{
int x,y;
};
point vertex[500000],res[500000];
int cmp(const void * a,const void* b)
{
point p1,p2;
p1=*(point*)a; p2=*(point*)b;
if(p1.x==p2.x)
return p1.y-p2.y;
return p1.x-p2.x;
}
int cross(point p0,point p1,point p2)
{
return (p2.y-p0.y)*(p1.x-p0.x)-(p1.y-p0.y)*(p2.x-p0.x);
}
int graham(int n)
{
int i,top,len;
qsort(vertex,n,sizeof(vertex[0]),cmp);
top=1; res[0]=vertex[0]; res[1]=vertex[1];
for(i=2;i<n;i++)
{
while(top && cross(res[top-1],res[top],vertex[i])<=0)
top--;
res[++top]=vertex[i];
}
len=top; res[++top]=vertex[n-2];
for(i=n-3;i>=0;i--)
{
while(top!=len && cross(res[top-1],res[top],vertex[i])<=0)
top--;
res[++top]=vertex[i];
}
return top-1;
}
int dist(point p1,point p2)
{
return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
int caculate(int n)
{
int i,j,k,max;
max=0;
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
k=dist(res[i],res[j]);
if(max<k) max=k;
}
return max;
}
int main()
{
int i,k,n;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%d%d",&vertex[i].x,&vertex[i].y);
n=graham(n);
k=caculate(n);
printf("%d\n",k);
}
return 0;
}



你可能感兴趣的:(poj)