poj2187 凸包上的最远点对的距离(凸包+旋转卡壳)
来源:互联网 发布:小米系统内存优化级别 编辑:程序博客网 时间:2024/05/01 13:39
很经典的一个求凸包上两个最远点对之间距离的题目。
一般都是用旋转卡壳做的。
求凸包可以用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 50010using 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
- poj2187 凸包上的最远点对的距离(凸包+旋转卡壳)
- Poj2187(最远点对距离)凸包+旋转卡壳
- poj2187 凸包+graham+旋转卡壳+最远点对
- POJ2187 Beauty Contest(最远点对,凸包+旋转卡壳)
- POJ2187-Beauty Contest- 最远点对问题(凸包+旋转卡壳)
- [计算几何][凸包][旋转卡壳] 最远距离点对
- POJ2187 n个点找距离最远的点对距离 graham 凸包
- POJ2187(旋转卡壳求平面最远点对)
- [Poj2187]Beauty Contest (旋转卡壳求最远点对)
- poj2187 Beauty Contest 平面最远点对(凸包)
- 凸包(旋转卡壳求最远点)
- POJ 2187 凸包的最远欧几里得距离:旋转卡壳算法
- POJ2187-最远点对->旋转卡壳(怎么开心怎么读)
- poj 2187 Beauty Contest 计算几何(凸包+旋转卡壳法)/最远点对
- POJ 2187 Beauty Contest(凸包_旋转卡壳之最远点对)
- POJ 2187 Beauty Contest (求最远点对,凸包+旋转卡壳)
- poj2187(最远点的距离的平方)
- 【凸包直径&平面最远点对&对锺点(旋转卡壳)】poj 2187 Beauty Contest
- 分布式搜索引擎Elasticsearch 插件汇总
- view 详解 (一) 将view加载到窗口过程分析
- Webkit之html解析(2)——解码和HTMLTokenizer的处理
- 特征选择常用算法综述
- h264 I帧的判断
- poj2187 凸包上的最远点对的距离(凸包+旋转卡壳)
- POJ 3624 Charm Bracelet
- 3D指环鼠标
- MFC工程的VS2008向VS2013移植
- 优先队列
- Xcode 5:一些使用提示和技巧
- Rotate Image
- Webkit之html解析(3)——HTMLToken的处理
- Ugly Numbers(1.5.8)