求凸包上最远两点距离的平方值
来源:互联网 发布:百度seo排名点击软件 编辑:程序博客网 时间:2024/04/18 19:45
#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cmath>using namespace std;typedef long long LL;const double INF = 1e18+5;const int MAXN = 1e6 + 5;const double eps = 1e-10;const double PI = acos(-1.0);int double_cmp(double x){ if(fabs(x) < eps) return 0; if(x > 0) return 1; return -1;}struct Point{ double x, y; int id; Point() {} Point (double _x, double _y, int i):x(_x),y(_y),id(i) {} bool operator <(const struct Point &tmp)const { if(double_cmp(x-tmp.x) == 0) return double_cmp(y-tmp.y) < 0; return double_cmp(x-tmp.x) < 0; } bool operator == (const struct Point &tmp)const { return double_cmp(x-tmp.x)==0&&double_cmp(y-tmp.y)==0; }} p[MAXN],st[MAXN];double XMulti(Point a, Point b, Point c)///ac X ab{ return (c.x-a.x)*(b.y-a.y) - (b.x-a.x)*(c.y-a.y);}double dis(Point a, Point b){ return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}double dis2(Point a, Point b){ return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);}double dot(Point a, Point b, Point c)///点积 ab . ac{ double s1 = b.x-a.x; double t1 = b.y-a.y; double s2 = c.x-a.x; double t2 = c.y-a.y; return s1*s2 + t1*t2;}int ConvexHull(Point *p, int n, Point *st)///凸包,返回凸包顶点个数{ sort(p, p+n); n = unique(p, p+n)-p;///去重 int m = 0; for(int i=0; i<n; i++) { while(m>1 && XMulti(st[m-2],p[i],st[m-1])<=0) m--; st[m++] = p[i]; } int k = m; for(int i=n-2; i>=0; i--) { while(m>k && XMulti(st[m-2],p[i],st[m-1])<=0) m--; st[m++] = p[i]; } if(n > 1) m--; return m;}double rotating_calipers(Point *p, int n)///旋转卡壳求凸包的直径,平面最远的点对{ int k=1; double ans = 0; p[n]=p[0]; for(int i=0; i<n; i++) { while(fabs(XMulti(p[i+1],p[k],p[i]))<fabs(XMulti(p[i+1],p[k+1],p[i]))) k=(k+1)%n; ans = max(ans,max(dis2(p[i],p[k]),dis2(p[i+1],p[k])));///注意是用的函数dis2 } return ans;}int main(){ int n; while(cin>>n) { for(int i=0; i<n; i++) { cin>>p[i].x>>p[i].y; } int m = ConvexHull(p, n, st); cout<<"m="<<m<<endl; double ans = rotating_calipers(st, m); printf("%0.0lf\n",ans); } return 0;}
0 0
- 求凸包上最远两点距离的平方值
- poj 2187 求凸包最远两点距离的平方值
- poj2187 凸包上的最远点对的距离(凸包+旋转卡壳)
- poj2187(最远点的距离的平方)
- 求凸包上最大距离 poj2187
- 求两点的距离
- 求两点的距离;
- 求两点的距离
- 求平面上N点最远两点和最近两点距离
- 求二叉树中最远的距离
- 求两点间的距离.
- 求两点间的距离
- 求两点之间的距离
- 求两点间的距离
- 求两点间的距离
- 求两点间的距离
- 求两点间的距离
- 求两点间的距离
- edit限制输入身份证号
- Windows下使用Sublime Text 3调试/运行 C/C++ 程序
- MySQL笔记-trigger与index
- Mycat生产实践---性能调优
- 在原型设计上,UI和UX设计师有哪三个区别?
- 求凸包上最远两点距离的平方值
- 关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation
- RecycleView加载不同条目
- 求凸包周长
- imx6 yocto编译备忘
- React-native学习过程 一 改变文字,添加图片
- RSS 教程
- 慎用百度!!!搜出来的资料坑死人!
- OOP/第三章 多态