Beauty Contest 【POJ 2187】【凸包+旋转卡壳】【旋转卡壳板子】

题目链接

题目大意

有n个点,求出距离最大的两个点之间的平方距离

解题思路

先求出来凸包,在凸包上跑旋转卡壳,求出最大距离

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int N=1e5+5;
struct node
{
    double x,y;
    node(){}
    node(double X,double Y)
    {
        x=X;
        y=Y;
    }
    node operator - (const node &a)
    {
        return node(x-a.x,y-a.y);
    }
};
node e[N],ch[N];
int n,m;

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

double cross(node a,node b)
{
    return a.x*b.y-b.x*a.y;
}

void Andrew()
{
    sort(e,e+n,cmp);
    m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1 && cross(ch[m-1]-ch[m-2],e[i]-ch[m-2])<=0.0)
            m--;
        ch[m++]=e[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k && cross(ch[m-1]-ch[m-2],e[i]-ch[m-2])<=0.0)
            m--;
        ch[m++]=e[i];
    }

    if(n>1)
        m--;
}

double dis(node a,node b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

double cross1(node p1,node p2, node p0)
{
    double x1=p1.x-p0.x;
    double y1=p1.y-p0.y;

    double x2=p2.x-p0.x;
    double y2=p2.y-p0.y;

    return(x1*y2-x2*y1);
}

double Diam()
{
    if(m==1)
        return dis(ch[0],ch[1]);
    int j=2;
    double ans=0;
    for(int i=0;i<m;i++)
    {
        while(cross1(ch[i],ch[i+1],ch[j])<cross1(ch[i],ch[i+1],ch[j+1]))
            j=(j+1)%m;
        ans=max(ans,max(dis(ch[i],ch[j]),dis(ch[i+1],ch[j])));
    }
    return ans;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%lf %lf",&e[i].x,&e[i].y);
    Andrew();
    printf("%d\n",(int)Diam());
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值