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. [hadoop] WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

    hadoop 启动后,有警告信息: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform ...

  2. C#分析搜索引擎URL得到搜索关键字,并判断页面停留时间以及来源页面

    前台代码: var start; var end; var state; var lasturl = document.referrer; start = new Date($.ajax({ asyn ...

  3. systemd的原理和适用方法

    systemd的原理: https://www.linux.com/learn/tutorials/527639-managing-services-on-linux-with-systemd htt ...

  4. devstack安装openstack

    devstack是目前安装OpenStack最为方便的工具,一般用于开发和测试OpenStack.如果想在生产环境安装的话,需要对 devstack做很多定制,或者使用puppet/chef等更成熟的 ...

  5. Visual对象之DrawingContext.DrawRectangle在有的状态下似乎并不能提高性能

    很多书上都提到使用Visual对象绘制图形可以提高绘图效率,但是经过本人亲测,结果却发现DrawingContext.DrawRectangle的效率远低于使用UIElement.Children.A ...

  6. SQL SERVER 2008安装时出现不能在控件上调用 Invoke 或 BeginInvoke错误 解决方法

    或者 SQL SERVER 2008安装时要求重启,但重启后仍要求重启.都可以使用此方法. 注册表的 "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet ...

  7. HDU3507 print artical

    题目大意:有N个数字a[N],每输出连续的一串,它的费用是 “这行数字的平方加上一个常数M”.问如何输出使得总费用最小.(n<=500000) 分析:动态规划方程为:dp[i]=dp[j]+M+ ...

  8. C语言中字符串常量到底存在哪了?

    常量存储总结局部变量.静态局部变量.全局变量.全局静态变量.字符串常量以及动态申请的内存区 1.局部变量存储在栈中2.全局变量.静态变量(全局和局部静态变量)存储在静态存储区3.new申请的内存是在堆 ...

  9. AngularJS PhoneCat代码分析

    转载自:http://www.tuicool.com/articles/ym6Jfen AngularJS 官方网站提供了一个用于学习的示例项目:PhoneCat.这是一个Web应用,用户可以浏览一些 ...

  10. Scala第一章学习笔记

    面向对象编程是一种自顶向下的程序设计方法.用面向对象方法构造软件时,我们将代码以名词(对象)做切割,每个对象有某种形式的表示服(self/this).行为(方法).和状态(成员变量).识别出名词并且定 ...