poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
2014-07-30 21:52
573 查看
/* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct Point{ Point(){} Point(int x, int y){ this->x=x; this->y=y; } int x, y; static int cross(Point a, Point b){ return a.x*b.y - a.y*b.x; } static int dist(Point a, Point b){ return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); } Point operator -(Point tmp){ return Point(x-tmp.x, y-tmp.y); } }; bool cmp(Point a, Point b){ if(a.x==b.x) return a.y<b.y; return a.x<b.x; } Point p[50005]; int ch[50005]; int n; int main(){ int i; while(scanf("%d", &n)!=EOF){ for(i=0; i<n; ++i) scanf("%d%d", &p[i].x, &p[i].y); sort(p, p+n, cmp); int m=0; //求下凸包, 如果某一个点不在线段之内,向量的叉积必定是<=0; for(i=0; i<n; ++i){ while(m>1 && Point::cross(p[ch[m-1]]-p[ch[m-2]], p[i]-p[ch[m-2]])<=0) m--; ch[m++]=i; } //为啥求上凸包的时候,坐标的从n-2开始:因为n-1点一定是在下凸包中的(因为它的横坐标最大,必然是包含其他节点的) int k=m; for(i=n-2; i>=0; --i){ while(m>k && Point::cross(p[ch[m-1]]-p[ch[m-2]], p[i]-p[ch[m-2]])<=0) m--; ch[m++]=i; } --m; int maxD=-1, j, d; for(i=0; i<m; ++i) for(j=i+1; j<=m; ++j) if(maxD < (d=Point::dist(p[ch[i]], p[ch[j]]))) maxD=d; printf("%d\n", maxD); } return 0; }
相关文章推荐
- poj 2187 凸包+平面上点之间最大距离
- POJ 2187 Beauty Contest(凸包:最远点对距离)
- POJ 2187(求凸包内两点之间最长距离)
- POJ 2187 Beauty Contest(凸包求最远点距离)
- POJ2187 Beauty Contest(凸包+对踵点对求解)
- Poj2187 凸包求最大距离
- 【计算几何】 Andrew凸包算法 + 旋转卡壳(以求点对最长距离为例) -- 以 POJ 2187 Beauty Contest 为例
- poj2187-Beauty Contest 求凸包最远距离(旋转卡壳算法模板题)
- poj 3608(旋转卡壳求解两凸包之间的最短距离)
- POJ 2187 Beauty Contest(凸包:最远点对距离)
- poj 3608(旋转卡壳求解两凸包之间的最短距离)
- Poj 2187 Beauty Contest_旋转凸包卡壳
- poj 2187 Beauty Contest(凸包+旋转卡壳)
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
- POJ_2187_Beauty Contest(凸包+暴力)
- POJ 2187 Beauty Contest (求最远点对,凸包+旋转卡壳)
- 编程之美 两个叶子的节点之间 最大距离 变种 leecode
- POJ 2187 Beauty Contest (凸包)
- poj2187 Beauty Contest 凸包求点集的直径
- poj 2187 Beauty Contest (凸包)