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. sql2008读取excel

    环境:win7(64位)+sql2008 sql语句: --启用Ad Hoc Distributed Queries: reconfigure reconfigure --使用完成后,关闭Ad Hoc ...

  2. 【原】Centos 7 下创建LVM流程

    阅读目录 个主分区,1个扩展分区] 或 [4个主分区],扩展分区又可以有多个分区:    window常见的分配方式:        方式1:[1个主分区(C盘)+1个扩展分区(包括3个分区,D,E, ...

  3. android学习笔记12——ListView、ListActivity

    ListView.ListActivity ==> ListView以垂直列表的形式显示所有列表项. 创建ListView的方式: 1.直接使用ListView创建 2.Activity继承Li ...

  4. TX Textcontrol 使用总结五——添加图片

    实现如图所示效果: 实现代码如下所示: 注意,此处不做代码格式化处理... using System;using System.Collections.Generic;using System.Dra ...

  5. 【转】C#中判断扫描枪输入与键盘输入

    提出问题:在收货系统中,常常要用到扫描枪扫描条码输入到TextBox,当条码无法扫描时,需要手工输入.如果是扫描枪输入时,我们将自动去判读条码,而手工输入时,最终需要加按回车键确认后判读条码.这时候我 ...

  6. Team Foundation Server

    概述 (通常记作“TFS”) 是一种为 Microsoft 产品提供 源代码管理. 数据收集. 报告和项目跟踪,而为协作 软件开发 的项目. 可作为独立的软件,或 Visual Studio Team ...

  7. 黄聪:发送键盘指令System.Windows.Forms.SendKeys.Send

    若要指定在按键(如 Enter 或 Tab)时不显示的字符,以及表示操作而不表示字符的键,请使用下表中的代码:   键 代码 Backspace {BACKSPACE}.{BS} 或 {BKSP} B ...

  8. 黄聪:wordpress前台自定义用户,调用wp_editor上传附件提示【抱歉,出于安全的考虑,不支持此文件类型】错误。

    1.直接禁用文件类型检测,在wp-config.php文件中,添加这样一句代码define('ALLOW_UNFILTERED_UPLOADS', true); 2.在functions.php里面, ...

  9. CE_现金模组基本概念(概念)

    2014-07-12 Created By BaoXinjian

  10. oProfile 学习

    oProfile工具可以分析CPU的负载量 只要对目标程序加上 -g 后重新编译,即可用oProfile进行分析 例如在测试apache的性能时, 增加 -g 编译选项[crifan@localhos ...