UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY。
纯暴力是会超时的,所以得另辟蹊径,用分治算法。
递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中的一块中,就会存在于跨越中线的点对中。
查找跨越中线的点比较麻烦,之前已经求出两块中的最小距离,只要在x范围在[m-d,m+d]的点中找对,更新最小距离,最后返回最小距离即可。
代码:
/*
* Author: illuz <iilluzen[at]gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: uva10245.cpp
* Lauguage: C/C++
* Create Date: 2013-09-04 19:57:26
* Descripton: UVA 10245 The Closest Pair Problem, partitation
*/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sqr(x) ((x) * (x))
#define dis(i, j) (sqrt(sqr(p[i].x - p[j].x) + sqr(p[i].y - p[j].y))) const int MAXN = 10010;
struct Point {
double x;
double y;
} p[MAXN];
int n, t[MAXN];
double Min; bool cmp1(Point a, Point b) {
return a.x < b.x;
} bool cmp2(int a, int b) {
return p[a].y < p[b].y;
} double solve(int l, int r) {
if (l == r) return 0xfffffff;
if (l + 1 == r) return dis(l, r);
int mid = (l + r) / 2;
double d = min(solve(l, mid), solve(mid + 1, r));
int c = 0;
for (int i = l; i <= r; i++)
if (fabs(p[mid].x - p[i].x) <= d)
t[c++] = i;
sort(t, t + c, cmp2);
for (int i = 0; i < c; i++)
for (int j = i + 1; j < c && p[t[j]].y - p[t[i]].y < d; j++) {
double td = dis(t[i], t[j]);
if (d - td > 1e-9) d = td;
}
return d;
} int main() {
while (scanf("%d", &n) && n) {
rep(i, n) scanf("%lf%lf", &p[i].x, &p[i].y);
sort(p, p + n, cmp1);
Min = solve(0, n - 1);
if (Min - 10000 <= 1e-9) printf("%.4lf\n", Min);
else printf("INFINITY\n");
}
return 0;
}
UVA 10245 The Closest Pair Problem 最近点问题 分治算法的更多相关文章
- UVA 10245 - The Closest Pair Problem
Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...
- UVA 10245 The Closest Pair Problem【分治】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269 题意: 求平面最近点对. 分析: 经典问题. n比 ...
- UVa 10245 The Closest Pair Problem (分治)
题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...
- uva 10245 The Closest Pair Problem_枚举
题意:求任意两点之间的距离的最少一个距离 思路:枚举一下就可以了 #include <iostream> #include<cstdio> #include<cmath& ...
- 2.11 2D平面最近点对问题[closest pair problem]
[本文链接] http://www.cnblogs.com/hellogiser/p/closest-pair-problem.html [题目] 给定平面上N个点的坐标,找出距离最近的两个点之间的距 ...
- uva10245-The Closest Pair Problem(平面上的点分治)
解析:平面上的点分治,先递归得到左右子区间的最小值d,再处理改区间,肯定不会考虑哪些距离已经大于d的点对,对y坐标归并排序,然后从小到大开始枚举更新d,对于某个点,x轴方向只用考虑[x-d,x+d]( ...
- HDU 6697 Closest Pair of Segments (计算几何 暴力)
2019 杭电多校 10 1007 题目链接:HDU 6697 比赛链接:2019 Multi-University Training Contest 10 Problem Description T ...
- Codeforces Round #185 (Div. 2) C. The Closest Pair 构造
C. The Closest Pair Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/p ...
- 【Codeforces Round #185 (Div. 2) C】The Closest Pair
[链接] 链接 [题意] 让你构造n个点,去hack一种求最近点对的算法. [题解] 让x相同. 那么那个剪枝就不会起作用了. [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] # ...
随机推荐
- 前端面试题整理(js)
1.HTTP协议的状态消息都有哪些? HTTP状态码是什么: Web服务器用来告诉客户端,发生了什么事. 状态码分类: 1**:信息提示.请求收到,继续处理2**:成功.操作成功收到,分析.接受3** ...
- .Net 社区虚拟大会”(dotnetConf)
.Net 社区虚拟大会”(dotnetConf) “.Net 社区虚拟大会”(dotnetConf) 2016 今天凌晨在Channel9 上召开,在Scott Hunter的30分钟的 Keynot ...
- QT显示机制(7篇相关文章)
了解QT显示机制,最重要的就是要了解QT是如何管理窗体的显示区域的,这里有个重要的类:QRegion, 在QT中可以通过QRegion定义一个窗体的显示区域,也可以通过QRegion定义窗体的可修改区 ...
- oracle数据库中insert与select
前几天遇到了一个问题,insert语句中,不仅要insert页面传过来的值,还要insert select的值. 语句应该这样写: insert into 表1(字段a,字段b,...) select ...
- MSSQL - 根据时间倒序删除第一行数据
delete top(1) from Tb_PaintOut where PaintNumber = (select top (1) PaintNumber from Tb_PaintOut orde ...
- MVC控制器里面使用dynamic和ExpandoObject
MVC控制器里面使用dynamic和ExpandoObject 在很多时候,我们在数据库里面定义表字段和实际在页面中展示的内容,往往是不太匹配的,页面数据可能是多个表数据的综合体,因此除了我们在表设计 ...
- android studio之argument for @notnull parameter 'name'
1)进入刚安装的Android Studio目录下的bin目录.找到idea.properties文件,用文本编辑器打开.2)在idea.properties文件末尾添加一行: disable.and ...
- TensorFlow实现与优化深度神经网络
TensorFlow实现与优化深度神经网络 转载请注明作者:梦里风林Github工程地址:https://github.com/ahangchen/GDLnotes欢迎star,有问题可以到Issue ...
- Spring mvc interceptor配置拦截器,没有登录跳到登录页
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 四种方法解决DIV高度自适应问题
本文和大家重点讨论一下解决DIV高度自适应的方法,这里主要从四个方面来向大家介绍,相信通过本文学习你对DIV高度自适应问题会有更加深刻的认识. DIV高度自适应 关于DIV高度的自适应,一直是个让人头 ...