POJ 2187 Beauty Contest

题目大意:

给n个点,求最远两点间距离的平方。

虽然这么写的,不要去枚举啊,枚举会超时的……


解题思路:

1、先用Graham扫描法求一个最大凸包,(求凸包的方法也是今天刚学的)

2、再用旋转卡壳法求凸包两点间的最大距离。


下面是代码:

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int x,y;
} point[50005],conhull[50005];
int cmp(const void *a,const void *b)
{
    struct node *aa=(struct node *)a;
    struct node *bb=(struct node *)b;
    if(aa->y==bb->y)return aa->x-bb->x;
    return aa->y-bb->y;
}
int n,cnt;
int xmult(node a,node b,node c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int dist(node a,node b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int max(int a,int b,int c)
{
    if(a<b)a=b;
    if(a<c)a=c;
    return  a;
}
void Graham()
{
    qsort(point,n,sizeof(point[0]),cmp);
    conhull[0]=point[0];
    conhull[1]=point[1];
    cnt=2;
    for(int i=2; i<n; i++)
    {
        while(cnt>1&&xmult(conhull[cnt-1],point[i],conhull[cnt-2])<=0)
        {
            cnt--;
        }
        conhull[cnt]=point[i];
        cnt++;
    }
    int min1=cnt;
    for(int i=n-2; i>=0; i--)
    {
        while(cnt>min1&&xmult(conhull[cnt-1],point[i],conhull[cnt-2])<=0)
        {
            cnt--;
        }
        conhull[cnt]=point[i];
        cnt++;
    }
}
int Rotating_Calipers()
{
    int i,j=1,ans=0;
    conhull[cnt-1]=conhull[0];
    for(i=0; i<cnt; i++)
    {
        while(xmult(conhull[i+1],conhull[j+1],conhull[i])>xmult(conhull[i+1],conhull[j],conhull[i]))
        {
            j=(j+1)%cnt;
        }
        ans=max(ans,dist(conhull[i],conhull[j]),dist(conhull[i+1],conhull[j+1]));
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d%d",&point[i].x,&point[i].y);
    }
    Graham();
    printf("%d\n",Rotating_Calipers());
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值