hdu1007 Quoit Design 平面最近点对(分治)
来源:互联网 发布:windows 10 更改键盘 编辑:程序博客网 时间:2024/05/17 04:21
题意:
就是平面最近点对←_←
思路:
先把点按x坐标排好序,利用分治的思想,比如要求区间[l, r]
的最近点对,先求区间[l, mid]
的最近点对,再求[mid + 1, r]
的最近点对,设此时的最近距离为best
。
但是best
没有考虑一个点在左边,一个点在右边的情况。此时我们再枚举区间[mid - best, mid + best]
中的所有点更新答案。
只不过这样裸枚举会超时,我们先把区间里的点放进一个temp数组,然后按y坐标排序,当两个点的y坐标之差超过best就没必要继续枚举了。
对了题目要求最后答案除以2。
代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <climits>#include <cmath>#define MAXN 100005#define INF 0x7fffffffusing namespace std;const double eps = 1e-8;struct Point{ double x, y; Point() {} Point(double a, double b) : x(a), y(b) {} bool operator < (const Point &p) const { return x < p.x; }};int n;Point p[MAXN];Point temp[MAXN];bool cmp(Point a, Point b) { return a.y < b.y; }double dist(int a, int b) { double t = (p[a].x - p[b].x) * (p[a].x - p[b].x) + (p[a].y - p[b].y) * (p[a].y - p[b].y); return t;}double dist(Point a, Point b) { double t = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); return t;}double dfs(int l, int r) { int mid = l + r >> 1; if (l + 1 == r) return dist(l, r); if (l + 2 == r) return min(dist(l, r), min(dist(l, l + 1), dist(l + 1, r))); double best = min(dfs(l, mid), dfs(mid + 1, r)); int tot = 0; for (int i = l; i <= r; i++) if (p[i].x >= p[mid].x - best && p[i].x <= p[mid].x + best) temp[tot++] = p[i]; sort(temp, temp + tot, cmp); for (int i = 0; i < tot; i++) for (int j = i + 1; j < tot; j++) if (temp[j].y - temp[i].y >= best) break; else best = min(best, dist(temp[i], temp[j])); return best;}int main() { while (~scanf("%d", &n) && n) { for (int i = 0; i < n; i++) scanf("%lf %lf", &p[i].x, &p[i].y); sort(p, p + n); double ans = sqrt(dfs(0, n - 1)) / 2; printf("%.2lf\n", ans); } return 0;}
0 0
- hdu1007 Quoit Design 平面最近点对(分治)
- hdu1007 Quoit Design【平面最近点对+分治】
- HDU1007 Quoit Design (平面最近点对)
- hdu1007 Quoit Design(最近点对)
- hdu1007 Quoit Design (最近点对)
- (hdu1007)Quoit Design,求最近点对
- HDU1007 Quoit Design(最近点对问题)
- hdu1007 Quoit Design(最近点对模板题)
- HDU1007-Quoit Design&POJ3714-Raid(最近点对问题)
- HDU1007:Quoit Design(分治)
- 传说中的hdu1007。Quoit Design,求最近点对~~
- Quoit Design(hdu1007)最近点对问题。模版哦!
- hdu 1007 Quoit Design(分治求最近点对)
- hdu 1007 Quoit Design 最近点对(分治)
- HDU 1007 Quoit Design(最近点对问题:分治)
- hdu 1007 Quoit Design 最近点对(分治)
- HDU1007——Quoit Design(最近点问题)
- hdu 1007 zoj 2107 Quoit Design 求平面最近点对 分治法
- JQuery处理json与ajax返回JSON实例
- C基础之数组
- 第一周上机实践项目4-图书馆的书
- atitit。win7 win8 win9 win10 win11 新特性总结与战略规划
- linux中nginx的安装(源码安装)及简单应用(一)
- hdu1007 Quoit Design 平面最近点对(分治)
- 通讯录管理
- 一些脚本
- Codeforces Round #296 (Div. 2) B. Error Correct System
- 排序算法小结
- MFC对话框型程序动态修改自身标题
- 制作网页几句话
- 分布式系统的事务处理转载代码的世界
- 3月20日课堂练习,for语句输出9*9乘法表