UVA 10245 - The Closest Pair Problem
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的更多相关文章
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- 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]( ...
- 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 ...
- HDU 6697 Closest Pair of Segments (计算几何 暴力)
2019 杭电多校 10 1007 题目链接:HDU 6697 比赛链接:2019 Multi-University Training Contest 10 Problem Description T ...
- UVa 100 - The 3n + 1 problem(函数循环长度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
随机推荐
- R(一): R基础知识
R 是一门拥有统计分析及作图功能的免费软件,主要用于数学建模.统计计算.数据处理.可视化等方向.据 IEEE Spectrum发布的2016年编程语言前10位排名来看,R语言由2015年排名第6位上升 ...
- 【jmeter】Bean shell使用(二)
上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...
- lucene.net helper类 【结合盘古分词进行搜索的小例子(分页功能)】
转自:http://blog.csdn.net/pukuimin1226/article/details/17558247 添加:2013-12-25 更新:2013-12-26 新增分页功能. ...
- Centos7和win7双系统调整默认启动
centos7之后都上grub2了,所以你要更改默认启动项什么的就不能像以前一样去改 /etc/grub.conf 当然你更不能去改/etc/grub2.conf 上了grub2之后,在设计有意规避让 ...
- 通过[蜘蛛协议]Robots.txt禁止搜索引擎收录的方法
什么是robots.txt文件? 搜索引擎通过一种程序robot(又称spider),自动访问互联网上的网页并获取网页信息. 您可以在您的网站中创建一个纯文本文件robots.txt,在这个文件中 ...
- 打开SDK Manager检查Android SDK下载和更新失败的解决方法
[故障描述] 打开SDK Manager检查Android SDK状况,出现以下情况: Failed to fetch URL https://dl-ssl.google.com/android/r ...
- 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Firmware综述
软件的层次关系(从底层到高层)如下: 1. PSP (Processor Support Package). A group of file that are specific to a CPU ty ...
- Ant -- Another Neat Tool
最早用来构建著名的Tomcat,可以看成是一个Java版本的Make.也正因为使用了Java,Ant是跨平台的. Ant有一个构建脚本build.xml <?xml version = ...
- rand()随机数的产生
#include "stdio.h"#include<stdlib.h>用下列公式即可得到指定范围[m,n]的随机数: r = rand()%(n - m + 1) + ...