Problem J
The Closest Pair Problem
Input: standard input
Output: standard output
Time Limit: 8 seconds
Memory Limit: 32 MB

Given a set of points in a two dimensional space, you will have to find the distance between the closest two points.

Input

The input file contains several sets of input. Each set of input starts with an integer N (0<=N<=10000), which denotes the number of points in this set. The next N line contains the coordinates of N two-dimensional points. The first of the two numbers denotes the X-coordinate and the latter denotes the Y-coordinate. The input is terminated by a set whose N=0. This set should not be processed. The value of the coordinates will be less than 40000 and non-negative.

Output

For each set of input produce a single line of output containing a floating point number (with four digits after the decimal point) which denotes the distance between the closest two points. If there is no such two points in the input whose distance is less than 10000, print the line INFINITY.

 

Sample Input

3
0 0
10000 10000
20000 20000
5
0 2
6 67
43 71
39 107
189 140
0

Sample Output

INFINITY
36.2215

很经典的算法,翻开任何一本算法书籍,分治算法那一节 。

STL的接口都是左闭又开 。  [)  。

合并的地方用到了  原地归并排序(上知网找) 。

#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int Max_N = ;
const double INF = 100000.0 ;
typedef pair<double ,double> P ;
bool cmp_y(P A ,P B){
return A.second < B.second ;
}
int N ;
P A[Max_N] ;
double close_pair(P *a ,int n){
if(n == )
return INF ;
int m = n>> ;
int x = a[m].first ;
double dist = min(close_pair(a,m),close_pair(a+m,n-m)) ;
inplace_merge(a,a+m,a+n,cmp_y) ; // 原地归并排序
vector<P>vec ;
for(int i = ; i < n ; i++){
if(fabs(a[i].first - x)>=dist)
continue ;
for(int j = vec.size() - ; j >= ; j--){
double dx = a[i].first - vec[j].first ;
double dy = a[i].second - vec[j].second ;
if(dy >= dist)
break ;
dist = min(dist,sqrt(dx*dx + dy*dy)) ;
}
vec.push_back(a[i]) ;
}
return dist ;
}
int main(){
int T ;
while(scanf("%d",&N)&&N){
for(int i = ; i < N ; i++)
scanf("%lf%lf",&A[i].first,&A[i].second) ;
sort(A,A+N) ;
double dist = close_pair(A,N) ;
if(dist - <= 1e-)
printf("%.4lf\n",close_pair(A , N)) ;
else
puts("INFINITY") ;
}
return ;
}

UVA 10245 - The Closest Pair Problem的更多相关文章

  1. UVA 10245 The Closest Pair Problem 最近点问题 分治算法

    题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...

  2. UVA 10245 The Closest Pair Problem【分治】

    题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269 题意: 求平面最近点对. 分析: 经典问题. n比 ...

  3. UVa 10245 The Closest Pair Problem (分治)

    题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...

  4. uva 10245 The Closest Pair Problem_枚举

    题意:求任意两点之间的距离的最少一个距离 思路:枚举一下就可以了 #include <iostream> #include<cstdio> #include<cmath& ...

  5. 2.11 2D平面最近点对问题[closest pair problem]

    [本文链接] http://www.cnblogs.com/hellogiser/p/closest-pair-problem.html [题目] 给定平面上N个点的坐标,找出距离最近的两个点之间的距 ...

  6. uva10245-The Closest Pair Problem(平面上的点分治)

    解析:平面上的点分治,先递归得到左右子区间的最小值d,再处理改区间,肯定不会考虑哪些距离已经大于d的点对,对y坐标归并排序,然后从小到大开始枚举更新d,对于某个点,x轴方向只用考虑[x-d,x+d]( ...

  7. 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 ...

  8. HDU 6697 Closest Pair of Segments (计算几何 暴力)

    2019 杭电多校 10 1007 题目链接:HDU 6697 比赛链接:2019 Multi-University Training Contest 10 Problem Description T ...

  9. UVa 100 - The 3n + 1 problem(函数循环长度)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

随机推荐

  1. [java] JVM监控与调优

    原文出处:http://www.cnblogs.com/zhguang/p/java-jvm-gc.html   光说不练假把式,学习Java GC机制的目的是为了实用,也就是为了在JVM出现问题时分 ...

  2. .NET去掉HTML标记

    using System.Text.RegularExpressions; /// <summary> /// 去除HTML标记 /// </summary> /// < ...

  3. sphinx 配置实例

    配置实例 3.1.数据源. 这里我们采用 mysql的数据源.具体情况如下: Mysql server:192.168.1.10 Mysql db :test Mysql 表:test.sphinx_ ...

  4. for循环内 执行$ajax(){}

    真是郁闷,在for 循环里添加了ajax异步传输之后,for循环是单线程处理,就是里面执行的是ajax,也不异步处理数据.而是执行完for循环的次数后,一起把ajax的数据处理掉. 解决办法.分开吧! ...

  5. Neutron Networking QoS

    目前,Neutron有一个QoS的proposal(https://wiki.openstack.org/wiki/Neutron/QoS#Documents),但是只有Ciscso和NVP插件实现了 ...

  6. div的打开与关闭js

    <script type="text/javascript"> var BoxHeight=$('.t_c').css("height"); //$ ...

  7. (转)用JQuery实现Fix表头表格

    本文转载自:http://www.cnblogs.com/evlon/archive/2009/06/12/1502239.html 我的技术要点: 1.用两个表,其中一个是表头,另一个是表格做表体 ...

  8. 黄聪:Discuz!X3.2 如何配置超级版主或者某些管理员,允许管理用户组或者权限

    点击后台-->站长-->后台管理团队-->新增用户(用户名)用户管理员即可

  9. C#继承的用法

    using System; namespace 继承 { public class cat { private string _name = null; private int _age = 0; p ...

  10. "this class is not key value coding-compliant for the key ..."问题的解决

    今天出现跟着MJ的思路敲的代码,自己最后运行出现这个 错误,发现是 自己在将属性和相关联的控件连线时出现了 错误.一开始取名时出现了错误,发现线连重复了. 在网上又找到了一些出现该类错误的相关解释: ...