poj2187 凸包上的最远点对的距离(凸包+旋转卡壳)

很经典的一个求凸包上两个最远点对之间距离的题目。

一般都是用旋转卡壳做的。

求凸包可以用Graham或者Andrew,都挺高效的。

旋转卡壳呢,先看一下它的思想介绍吧。

下面这个链接把旋转卡壳讲的很清楚。

http://www.cnblogs.com/xdruid/archive/2012/07/01/2572303.html

这个链接是旋转卡壳算法的一些其它典型运用

http://blog.csdn.net/hanchengxi/article/details/8639476

好了,其实我是打酱油的,这道题就是一个模板水题。主要是通过这个题来初步认识旋转卡壳算法的思想和不同的运用。

在实现的时候,要根据具体情况稍微调整实现手段,但是思想本质上都是一样的!

本题代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define MAX 50010
using namespace std;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {}
};
Point point[MAX],ch[MAX];
double ans;
int N;
typedef Point Vector;
Vector operator + (Vector A, Vector B) {return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Point A, Point B) {return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A, double p) {return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A, double p) {return Vector(A.x/p,A.y/p);}
bool operator < (const Point &a,const Point &b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);}
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
bool operator ==(const Point& a,const Point& b)
{
    return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;} //点积
double Length(Vector A) {return (Dot(A,A));}  //向量长度
double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));} //向量夹角
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;} //叉积

//计算凸包,出入点数组p,个数为p,输出点数组ch,函数返回凸包顶点数。
//输入不能有重复点。函数执行完之后输入点的顺序被破坏
//如果不希望在凸包的边上有输入点,把两个<=改成<
//在精度要求高的时候,用dcmp比较
int ConvexHull(Point *p,int n)
{
    sort(p,p+n);
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}
double rotating_calipers(int n)  //旋转卡壳,(比面积来做,因为是单调函数。)
{
    int q=1;
    double ret=0.0;
    ch[n]=ch[0];
    for(int p=0;p<n;p++)
    {
      while(abs(Cross(ch[p+1]-ch[p],ch[q+1]-ch[p]))>abs(Cross(ch[p+1]-ch[p],ch[q]-ch[p])))
            q=(q+1)%n;
      ret=max(ret,max(Length(ch[p]-ch[q]),Length(ch[p+1]-ch[q])));
    }
    return ret;
}
int main()
{
    int i,j;
    scanf("%d",&N);
    for(i=0;i<N;i++)
        scanf("%lf%lf",&point[i].x,&point[i].y);
    int num=ConvexHull(point,N);
    ans=rotating_calipers(num);
    printf("%0.0lf\n",ans);
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值