hdu 1007 Quoit Design (最近点对、分治)
2011-08-15 11:14
363 查看
hdu 1007 Quoit Design (最近点对)
http://acm.hdu.edu.cn/showproblem.php?pid=1007
“最近点对”的经典入门题
所用到的思想和“归并排序”类似, 分治再合并。
http://acm.hdu.edu.cn/showproblem.php?pid=1007
“最近点对”的经典入门题
所用到的思想和“归并排序”类似, 分治再合并。
#include "stdio.h" #include "cmath" #include "iostream" #include "algorithm" using namespace std; struct point {double x, y;}; point p[100003]; point tp[100003]; bool cmp_x(const point & a, const point & b) { return a.x < b.x; } bool cmp_y(const point & a, const point & b) { return a.y < b.y; } double min(double a, double b) { return a < b ? a : b; } double dis(const point & a, const point & b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } double solve(int lf, int rt, int n) //left, right { if(lf == rt) return 1e9; //只有一个点时,距离设为无穷大 if(rt - lf == 1) return dis(p[lf], p[rt]); int mid = (lf + rt) >> 1; double ans = min(solve(lf, mid, n), solve(mid + 1, rt, n)); int lp = 0, i, j; for(i = mid; i >= lf && p[i].x - p[mid].x < ans; i--) { tp[lp].x = p[i].x; tp[lp++].y = p[i].y; } for(i = mid + 1; i <= rt && p[i].x - p[mid].x < ans; i++) { tp[lp].x = p[i].x; tp[lp++].y = p[i].y; } sort(tp, tp + lp, cmp_y); for(i = 0; i < lp; i++) for(j = i + 1; j - i < 7 && j < lp; j++) ans = min(ans, dis(tp[i], tp[j])); return ans; } int main() { int n, i; while(scanf("%d", &n) && n) { for(i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y); sort(p + 1, p + n + 1, cmp_x); printf("%.2lf\n", solve(1, n, n) / 2.0); } return 0; }
相关文章推荐
- HDU 1007 Quoit Design(最近点对问题:分治)
- hdu 1007 Quoit Design 分治求最近点对
- HDU - 1007 - Quoit Design (分治解平面最近点对)
- 【HDU 1007 】Quoit Design 【分治--最近点对问题】
- hdu 1007 Quoit Design(分治求最近点对)
- hdu 1007 Quoit Design 最近点对(分治)
- hdu 1007 Quoit Design 最近点对(分治)
- hdu 1007 Quoit Design 分治求最近点对
- hdu 1007 zoj 2107 Quoit Design 求平面最近点对 分治法
- HDU 1007-Quoit Design (平面最近点对->分治)
- hdu 1007 Quoit Design (最近点对、分治)
- 【分治 求最近点对】hdu 1007 Quoit Design
- hdu 1007 Quoit Design 最近点对
- HDU 1007 Quoit Design(计算几何 平面最近点对)
- Hdu 1007 Quoit Design【最近点对】
- HDU 1007 Quoit Design_二分法求最近点对
- 【几何】HDU 1007 Quoit Design 最近点对
- hdu 1007 平面最近点对 分治
- 杭电ACM OJ 1007 Quoit Design 最近点对 分治 递归
- HDU 1007 Quoit Design 分治