POJ 2187 Beauty Contest (凸包)

本文介绍使用Andew算法解决凸包问题,并通过枚举找到凸包中两点间的最大距离。代码实现包括点结构定义、距离计算、交叉乘等关键函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:POJ 2187

凸包第一发。。用的大白书上的andew算法。

先求出凸包,然后最大距离一定是凸包之中的某两点之间的距离,然后枚举找出最大值。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int top, n;
struct Point
{
    int x, y;
}p[100000], tu[100000];
int dist(Point x, Point y)
{
    return (x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y);
}
Point operator - (Point x, Point y)
{
    Point z;
    z.x=x.x-y.x;
    z.y=x.y-y.y;
    return z;
}
int Cross(Point x, Point y)
{
    return x.x*y.y-x.y*y.x;
}
int cmp(Point x, Point y)
{
    if(x.x==y.x) return x.y<y.y;
    return x.x<y.x;
}
int Max(int x, int y)
{
    return x>y?x:y;
}
void Andew()
{
    int i, j, k;
    sort(p,p+n,cmp);
    top=0;
    for(i=0;i<n;i++)
    {
        while(top>1&&Cross(tu[top-1]-tu[top-2],p[i]-tu[top-1])<=0) top--;
        tu[top++]=p[i];
    }
    k=top;
    for(i=n-2;i>=0;i--)
    {
        while(top>k&&Cross(tu[top-1]-tu[top-2],p[i]-tu[top-1])<=0) top--;
        tu[top++]=p[i];
    }
    int max1=-1;
    for(i=0;i<top;i++)
    {
        for(j=0;j<i;j++)
        {
            max1=Max(max1,dist(tu[i],tu[j]));
        }
    }
    printf("%d\n",max1);
}
int main()
{
    int i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&p[i].x,&p[i].y);
    }
    Andew();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值