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相同. 那么那个剪枝就不会起作用了. [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] # ...
随机推荐
- iOS经常使用加密方式(MD5,AES,BASE64)与网络数据安全
演示样例项目下载地址 https://github.com/cerastes/Encryption 1MD5 创建MD5类 #import <Foundation/Foundation.h&g ...
- PHP函数详细剖析之rtrim函数 By ACReaper
string rtrim ( string $str [, string $charlist ] ) 这个函数很好理解.r表示右边.trim表示修剪.即右边修剪.默认修剪字符str右边的字符.默认修剪 ...
- switch的方便用法
int ch = getch(); switch(ch) { case '0' ... '9': if (in_count) { count = count * 10 + (ch - '0'); } ...
- webview 加载某些网页失败的处理办法(第七条)
1.添加权限:AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则会出Web page not available错 ...
- StreamWrite-StreamRead 读写文本文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C# 未能加载文件或程序集“MySQLDriverCS..." 错误解决
在解决方案的属性里,生成,里面有个目标平台,网上说的 大概也就是64位和32位的不兼容问题..试着把目标平台改为X86后竟然神奇的正常了!
- Leetcode: Median of Two Sorted Arrays. java.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- ASP.NET - 页面传值 Request.QuerString[].ToString();
public partial class WebForm2 : System.Web.UI.Page { BLL.CategoryBLL categorybll = new CategoryBLL() ...
- ABAP 向上取整和向下取整 CEIL & FLOOR
下面是一段关于CEIL 和 FLOOR 的代码 DATA:a TYPE mseg-menge, b TYPE mseg-menge, c TYPE mseg-menge. a = '1.36'. b ...
- Android开发之查看应用包名package和入口activity名称的方法
使用android自动化测试工具monkeyrunner启动应用时,需要填写被测程序的包名和启动的Activity,以下有两种查看应用包名package和入口activity名称的方法: 方法一:使用 ...