poj_2187求凸包直径

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point{
    int x,y;
    Point(int x=0,int y=0):x(x),y(y){}
};
typedef Point Vector;
bool cmp(const Point &p,const Point &q)
{
    if(p.x!=q.x) return p.x<q.x;
    return p.y<q.y;
}
Vector operator -(Point A,Point B){return Vector(A.x-B.x,A.y-B.y);}
Point p[50010],ch[50010];
int Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
int ConvexHull(Point *p,int n,Point *ch)
{
    sort(p,p+n,cmp);
    int m=0;
    for(int i=0;i<n;i++){
        while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
    ch[m++]=p[i];}
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
        ch[m++]=p[i];
    }
    if(n>1)
        m--;
    return m;
}
int Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}
int Length(Vector A){return Dot(A,A);}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        //memset(ch,0,sizeof(ch));
       for(int i=0;i<n;i++)
            scanf("%d%d",&p[i].x,&p[i].y);
       int x=ConvexHull(p,n,ch);
        int ans=0;
        for(int i=0;i<x-1;i++)
            for(int j=i+1;j<x;j++)
            {if(Length(ch[i]-ch[j])>ans)
                ans=Length(ch[i]-ch[j]);
             //if(Length(ch[i]-ch[j])<Length(ch[i]-ch[j-1]))
                //break;
                }
            printf("%d\n",ans);
    }
    return 0;
}

posted @ 2014-08-21 13:58  JarvisLau  阅读(170)  评论(0编辑  收藏  举报