http://poj.org/problem?id=2187
求凸包
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 50001
using namespace std;
struct point
{
int x;
int y;
}po[M],stack[M];
bool cmp(point a,point b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
__int64 Getlen(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)
+(a.y-b.y)*(a.y-b.y);
}
bool mult(point sp,point ep,point op)
{
return (sp.x-op.x)*(ep.y-op.y)>=
(ep.x-op.x)*(sp.y-op.y);
}
int Graham(point pnt[],int n,point res[])
{
int i,len,k=0,top=1;
sort(pnt,pnt+n,cmp);
if(n==0) return 0;
res[0]=pnt[0];
if(n==1) return 1;
res[1]=pnt[1];
if(n==2) return 2;
res[2]=pnt[2];
for(i=2;i<n;i++)
{
while(top && mult(pnt[i],res[top],res[top-1]))
top--;
res[++top]=pnt[i];
}
len=top;
res[++top]=pnt[n-2];
for(i=n-3;i>=0;i--)
{
while(top!=len && mult(pnt[i],res[top],res[top-1]))
top--;
res[++top]=pnt[i];
}
return top;
}
int main()
{
int i,j;
int top;
int n;
__int64 nmax,length;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%d%d",&po[i].x,&po[i].y);
top=Graham(po,n,stack);
nmax=-1;
for(i=0;i<top;i++)
{
for(j=0;j<top;j++)
{
length=Getlen(stack[i],stack[j]);
if(length>nmax)
nmax=length;
}
}
printf("%I64d\n",nmax);
}
}
/*
13
3 0
4 0
5 0
5 2
6 5
3 1
5 4
4 6
7 3
1 5
3 4
2 3
1 2
*/