旋卡求凸包直径

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 50005;
struct point
{
    int x,y;
    bool operator<(const point a)const
    {
        return y < a.y ||(y == a.y && x < a.x);
    }
} PointSet[maxn],ch[maxn];
int multiply(point s,point e,point o)
{
    return ((s.x-o.x)*(e.y-o.y)-(s.y-o.y)*(e.x-o.x));
}

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

bool cmp(point a,point b)
{
    if(multiply(a,b,PointSet[0]) == 0)
        return dist2(a,PointSet[0]) < dist2(PointSet[0],b);
    return multiply(a,b,PointSet[0]) > 0;
}

int rotating_calipers(point *ch,int n)
{
    int q=1,ans=0;
    ch[n]=ch[0];
    for(int p=0; p<n; p++)
    {
        while(multiply(ch[p+1],ch[q+1],ch[p]) > multiply(ch[p+1],ch[q],ch[p]))
            q=(q+1)%n;
        ans=max(ans,max(dist2(ch[p],ch[q]),dist2(ch[p+1],ch[q+1])));
    }
    return ans;
}
void convex_hull(point *p,point *ch,int n,int &len)
{
    sort(p, p+n);
    ch[0]=p[0];
    ch[1]=p[1];
    int top=1;
    for(int i=2; i<n; i++)
    {
        while(top>0 && multiply(ch[top],p[i],ch[top-1])<=0)
            top--;
        ch[++top]=p[i];
    }
    int tmp=top;
    for(int i=n-2; i>=0; i--)
    {
        while(top>tmp && multiply(ch[top],p[i],ch[top-1])<=0)
            top--;
        ch[++top]=p[i];
    }
    len=top;
}
int main()
{
    int n,len;
    while(scanf("%d",&n)!=EOF)
    {
        memset(ch,0,sizeof(ch));
        for(int i=0; i<n; i++)
            scanf("%d%d",&PointSet[i].x,&PointSet[i].y);
        convex_hull(PointSet,ch,n,len);
        printf("%d\n",rotating_calipers(ch,len));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值