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相同. 那么那个剪枝就不会起作用了. [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] # ...
随机推荐
- 反应堆Reactor
mvn -h 可以看到很多命令及其用途:-am --also-make 同时构建所列模块的依赖模块:-amd -also-make-dependents 同时构建依赖于所列模块的模块:-pl --pr ...
- 关于PEER - PEER毅恒挚友 - Powered by Discuz!
关于PEER - PEER毅恒挚友 - Powered by Discuz! PEER Experience Exchange Rostrum(PEER)由海外中国留学生和中国知名高校学生组成,服务中 ...
- Android 系统api实现定位及使用百度提供的api来实现定位
目前在国内使用定位的方法主要是 1. Android系统提供的 LocationManager locationManager = (LocationManager) getSystemService ...
- 菜鸟学SSH(十一)——Hibernate之SchemaExport+配置文件生成表结构
今天说点基础的东西,说说怎样通过SchemaExport跟Hibernate的配置文件生成表结构.事实上方法很easy,仅仅须要两个配置文件,两个Java类就能够完毕. 首先要生成表,得先有实体类,以 ...
- HDU 2045 不easy系列之(3)—— LELE的RPG难题
思路: 1.若前n-1位涂的颜色是符合条件的,则因为首尾不同,再加入一位时,仅仅有1种方法:即s[n] = s[n-1] 2.若前n-1位组成的串不符合,再加入一位后合法.即由于首尾同样而引起的不合法 ...
- EasyUI - 后台管理系统 - 增加,删除,修改
效果: html代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ad ...
- MyBatis深入理解一
Mybatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .iB ...
- 将 Shiro 作为一个许可为基础的应用程序 五:password加密/解密Spring应用
考虑系统password的安全,眼下大多数系统都不会把password以明文的形式存放到数据库中. 一把会採取下面几种方式对password进行处理 password的存储 "编码" ...
- Android 百度地图 SDK v3.0.0 (三) 加入覆盖Marker与InfoWindow使用
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/37737213 上篇博客已经实现了地图的定位以及结合了方向传感器用户路痴定位方向, ...
- 外国的Delphi网站
www.phidels.com delphifr.com http://www.swissdelphicenter.com/torry/showcode.php?id=787 B4A delphifa ...