凸包问题 poj 2187

【题意】:给出n个点的坐标 求这里面两点距离的平方最大的 输出其值

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct point{int x;int y;};

point p[60000],ch[60000],v,w;
int m,n;

int cmp(point a,point b)
{
    if(a.x!=b.x)    
        return a.x<b.x;
    return a.y<b.y;
}

int cross(point a,point b,point c)
{
    w.x=a.x-b.x;     w.y=a.y-b.y;
    v.x=c.x-b.x;     v.y=c.y-b.y;
    return w.x*v.y-w.y*v.x;
}

int tubao()
{ 
    int i;
    m=0;
    sort(p,p+n,cmp);
    
    for(i=0;i<n;i++)
    {    
        while(m>1&&cross(ch[m-1],ch[m-2],p[i])<=0)   //先判断 m>1 再判断后面的
        {
        
            m--;
        }
        ch[m].x=p[i].x;   ch[m++].y=p[i].y;
    }
    int k=m;
    for(i=n-2;i>=0;i--)
    {
        while(m>k&&cross(ch[m-1],ch[m-2],p[i])<=0)   //
            m--;
        ch[m].x=p[i].x;   ch[m++].y=p[i].y;
    }
    if(n>1)
        m--;
    return m;
}

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

int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)
            scanf("%d%d",&p[i].x,&p[i].y);
        tubao();
        int ans=0;
        for(i=0;i<m;i++)
            for(j=i+1;j<m;j++)
            {
                int t=length(ch[i],ch[j]);
                if(t>ans)
                    ans=t;
            }
            printf("%d
",ans);
    }
    return 0;
}
        
原文地址:https://www.cnblogs.com/assult/p/3347749.html