平面内距离最近的两个点-- 分治法
2016-12-29 20:22
459 查看
题目地址 hdu 1007
http://acm.hdu.edu.cn/showproblem.php?pid=1007题目描述
Problem DescriptionHave you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.
Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.
Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.
Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
Sample Input
2 0 0 1 1 2 1 1 1 1 3 -1.5 0 0 0 0 1.5 0
Sample Output
0.71 0.00 0.75
ac代码
分治法,参考https://my.oschina.net/bgbfbsdchenzheng/blog/498823
#include <cstdio> #include <memory> #include <cassert> #include <cstdlib> #include <cmath> #include <cstring> #include <iostream> #include <string> #include <vector> #include <queue> #include <algorithm> #include <sstream> #include <list> #include <stack> #include <map> #include <set> #include <unordered_map> using namespace std; #define INF 0x7fffffff struct Pos{ double x; double y; }; double Dis(Pos p1, Pos p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y)); } bool cmp(Pos p1, Pos p2) { if(p1.x == p2.x) return p1.y < p2.y; return p1.x < p2.x; } double smaller(double p, double q) { return (p > q) ? q : p; } int n; vector<Pos> v; double b2(int left, int right) { if(left >= right) return 0; if(right == left + 1) return Dis(v[left], v[right]); int mid = (right - left) / 2 + left; double d = smaller( b2(left ,mid), b2(mid, right)); for(int i=mid-1;i>= left&& v[mid].x - v[i].x < d; i--) { for(int j=mid +1;j<=right && v[j].x - v[mid].x < d && fabs(v[i].y - v[j].y) < d; j ++) { double disTmp = Dis(v[i], v[j]); if(disTmp < d) { d = disTmp; } } } return d; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(scanf("%d", &n) != EOF && n != 0) { v.clear(); Pos ptmp; for(int i = 0;i<n;i++) { scanf("%lf%lf", &ptmp.x,&ptmp.y); v.push_back(ptmp); } if(n == 1){ printf("0.00\n"); }else{ sort(v.begin(), v.end(), cmp); double d = b2(0, n-1); printf("%.2lf\n", d * 0.5); } } return 0; }
相关文章推荐
- 平面中有若干个点,寻找距离最近的两个点,输出其编号
- 最近距离 已知平面上的若干点的位置,存入一个List中。现在需要计算所有这些点中,距离最近的两个点间的最小距离
- 最近距离 已知平面上的若干点的位置,存入一个List中。现在需要计算所有这些点中,距离最近的两个点间的最小距离。
- 分治法求解平面n点中距离最近的两点
- 分治,在平面上找距离最近的两个点
- 最近距离 已知平面上的若干点的位置,存入一个List中。现在需要计算所有这些点中, 距离最近的两个点间的最小距离。请补全缺失的代码。
- 分治法解决平面上N点最近2点距离———算法应该OK~~~
- 找出平面上距离最近的两个点
- 给定平面上的N个点,寻找距离最远的两个点
- tyvj 1081 最近距离 平面最近点对
- poj 3608 Bridge Across Islands 旋转卡壳(两个凸包的最近距离)
- [算法] 已知在平面坐标系内有N个点,求离开给定坐标距离最近的10个点
- 用分治法输出一个一维数组中的n个不同实数间距离最小的两个实数
- 平面最近距离点对
- HDU1007(求最近两个点之间的距离)
- hdu 1007 Quoit Design 找最近的两个点的距离
- 初学算法-分治法求平面上最近点对(Closest Pair)-HDU 1007
- (Relax 数论1.14)POJ 26889 Prime Distance(区间筛素数:求一个区间L\U中距离最近的两个素数和距离最远的两个素数)
- poj3714 两个阵营的 平面最近点对
- 分治法:求解一个乱序数组中距离最近的两个数