poj 2187 Beauty Contest 凸包最长直径

                 题意:求所有点中最远距离,即凸包的最长直径

                 思路:求凸包,旋转卡壳求最长直径,枚举各个点,找对踵点,更新距离

                 代码:

                 

#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<map>
#include<cstdio>
#include<cstring>
#include<math.h>
#define sqr(x)  ((x)*(x))

using namespace std;
const double eps=1e-8;
const double pi=acos(-1.0);
int dcmp(double x)
{
    if(x<-eps) return -1;
    else return x>eps;
}
struct cpoint
{
    double x,y;
    cpoint(){}
    cpoint (double x,double y) : x(x),y(y) {}
    cpoint operator + (const cpoint &u) const {
        return cpoint(x + u.x, y + u.y);
    }
    cpoint operator -(const cpoint &u) const
    {
       return cpoint (x-u.x,y-u.y);
    }
    double operator * (const cpoint &u) const
    {
        return x*u.y-y*u.x;
    }
    double operator ^ (const cpoint &u) const
    {
        return x*u.x+y*u.y;
    }
    bool operator == (const cpoint &u) const
    {
        return dcmp(x-u.x)==0&&dcmp(y-u.y)==0;
    }
};
double cross(cpoint o,cpoint p,cpoint q)
{
    return (p-o)*(q-o);
}
double dis(cpoint p,cpoint q)
{
    return sqrt(sqr(p.x-q.x)+sqr(q.y-p.y));
}
double dissqr(cpoint p,cpoint q)
{
    return sqr(p.x-q.x)+sqr(q.y-p.y);
}
cpoint bp;
int PolarCmp(const cpoint &p1,const cpoint &p2)
{
    int u=dcmp(cross(bp,p1,p2));
    return u>0 || (u==0&&dcmp(dissqr(bp,p1)-dissqr(bp,p2))<0);
}
void graham(cpoint pin[],int n,cpoint ch[],int &m)
{
    int i,j,k,u,v;
    memcpy(ch,pin,n*sizeof(cpoint));
    for(i=k=0;i<n;i++)
    {
        u=dcmp(ch[i].x-ch[k].x);
        v=dcmp(ch[i].y-ch[k].y);
        if(v<0||(v==0&&u<0))
             k=i;
    }
    bp=ch[k];
    sort(ch,ch+n,PolarCmp);
    n=unique(ch,ch+n)-ch;
    if(n<=1) { m=n;return ;}
    if(dcmp(cross(ch[0],ch[1],ch[n-1]))==0)
    {
        m=2;ch[1]=ch[n-1];return ;
    }
    ch[n++]=ch[0];
    for(i=1,j=2;j<n;++j)
       {
           while(i>0&&dcmp(cross(ch[i-1],ch[i],ch[j]))<=0) i--;
           ch[++i]=ch[j];
       }
       m=i;
}
double rotating (cpoint cp[],int n)
{
    int i=1;
    double res=0.0;
    cp[n]=cp[0];
    for(int j=0;j<n;++j)
    {
      while (dcmp(fabs(cross(cp[i+1], cp[j], cp[j+1]))-
                fabs(cross(cp[i], cp[j], cp[j+1])) ) > 0)
            i = (i + 1) % n;
        res = max(res, max(dissqr(cp[i], cp[j]),
                dissqr(cp[i+1], cp[j+1])));
    }
    return res;
}
cpoint dp[50010],ch[50010];
int main()
{

    int n,m;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
          scanf("%lf %lf",&dp[i].x,&dp[i].y);
        graham(dp,n,ch,m);
        double ans=rotating(ch,m);
        printf("%d\n",(int)(ans+0.5));
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值