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相同. 那么那个剪枝就不会起作用了. [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] # ...
随机推荐
- Coin Toss
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=31329#problem/G 使用二维数组f[ i ] [ j ] 表示前i 位中有j个 ...
- Winsock基础编程
Winsock基础编程 Socket的英文原义是"孔"或"插座".作为BSD UNIX的进程通信机制,取后一种意思.通常也称作"套接字",用 ...
- 基于visual Studio2013解决面试题之0804复杂链表
题目
- socket为send和recv设置超时时间
linux和windows下用setsockopt设置SO_SNDTIMEO,SO_RCVTIMEO的参数的一点区别 UDP的socket在某些情况:如对方关闭时,本地可能sendto不出去数据,然后 ...
- UVA 10201 Adventures in Moving - Part IV(dp)
Problem A: Adventures in Moving - Part IV To help you move from Waterloo to the big city, you are co ...
- Hadoop基于文件的数据结构及实例
基于文件的数据结构 两种文件格式: 1.SequenceFile 2.MapFile SequenceFile 1.SequenceFile文件是Hadoop用来存储二进制形式的<key,val ...
- java 变长參数使用原则
1.java变长參数用...表示,如Print(String... args){ ... }; 2.假设一个调用既匹配一个固定參数方法.又匹配一个变长參数方法,则优先匹配固定參数的方法 3.假设一个 ...
- 从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
一.C语言错误处理方法 1.返回值(if … else语句判断错误) 2.errno(linux 系统调用) 3.goto语句(函数内局部跳转) 4.setjmp.longjmp(Do not use ...
- EXT2/EXT3文件系统(二)
整理自<鸟哥的Linux私房菜>,整理者:华科小涛http://www.cnblogs.com/hust-ghtao/ 接EXT2/EXT3文件系统(一): 2.3 Supe ...
- 测试framebuffer
static GGLContext *gr_context = 0; static GGLSurface gr_framebuffer[2]; static unsigned gr_active_fb ...