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. 详解@Autowired、@Qualifier和@Required

    A.@Autowired org.springframework.beans.factory.annotation.Autowired public @interface Autowired Mark ...

  2. CCS float vs clear

    有人已经写过了.(*^__^*) 嘻嘻…… 为啥我不能写, ( ‵o′)凸 float 首先,HTML的布局是流布局.其元素是分为行内元素和块级元素的. 所谓行内元素就是接着写不会发生换行的元素如&l ...

  3. visual studio 编译时 出现 Files 的值 乱码

    参考了:http://blog.163.com/jiang_tao_2010/blog/static/121126890201031031337332/ 最近在做程序时,在生成解决方法过程中,电脑出现 ...

  4. vue通过判断写样式(v-bind)

    v-bind:style="$index % 2 > 0?'background-color:#FFF;':'background-color:#D4EAFA;'"

  5. RESTful API 简书

    RESTful API 概述 参考地址 RESTful架构是一种流行的互联网软件架构,它结构清晰,符合标准,易于理解,扩展方便.REST是Representational State Transfer ...

  6. Android反向工程需要的几个软件

    1.apktoolapktool d xxx.apk 得到全部的资源素材 2.dex2jardex2jar classes.dex 3.jd-gui把jar文件转成 .java的源代码

  7. 访问修饰符internal

    internal(C# 参考) internal 关键字是类型和类型的成员 访问修饰符. 只有在同一程序集的文件中,内部类型或成员才是可访问的,如下例所示: public class BaseClas ...

  8. ant中copy操作学习心得(转)

    Ant真是太方便了,以前都没注意到它.功能很强大,能创建数据库,配置服务器,部署发布应用……只需要写好build.xml文件,剩下的就交给ant来“安装”你的WEB应用了. Appfuse的第一个an ...

  9. VC++、MFC、COM和ATL的区别

    今天看到的,感觉不错.转载了 一.什么是MFC 微软基础类(Microsoft Foundation Classes),实际上是微软提供的,用于在C++环境下编写应用程序的一个框架和引擎,VC++是W ...

  10. Codeforces Round #356 (Div. 2) C. Bear and Prime 100(转)

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...