题目链接:

http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269

题意:

求平面最近点对。

分析:

经典问题。

n比较大,直接枚举不可。

与上一道的树分治类似,我们也可以将点按照x坐标分成两类。

假设把所有点按照x坐标分成两类,那么有如下两种情况:

  1. 点p,q同属于左半边
  2. 点p,q一个属于左边一个属于右边

同样,对于第一种情况我们采用递归即可求解。

对于第二种情况,由于已经知道第一种情况下的最小距离d,所以我们只需考虑到划分的直线(x坐标为x0)的距离小于d的点,即坐标满足x0−d<x<x0+d,而对于y坐标,每个点只需考虑y坐标比自己大的,且相差不超过d的点即可。

代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstdio>
using namespace std;
#define x first
#define y second
typedef pair<double, double>p;
const int maxn = 1e4 + 5, oo = 0x3f3f3f3f;
int n;
double eps = 1e-8;
p a[maxn];
bool cmp(p a, p b){return a.y < b.y;}
double solve(p* a, int n)
{
if(n <= 1) return oo;
int m = n / 2;
double xx = a[m].x;
double d = min(solve(a, m), solve(a + m, n - m));
vector<p>b;
for(int i = 0; i < n; i++){
if(fabs(a[i].x - xx) <= d) b.push_back(a[i]);
}
sort(b.begin(), b.end(), cmp);
for(int i = 0; i <b.size(); i++){
for(int j = i + 1; j < b.size(); j++){
double dx = b[j].x - b[i].x;
double dy = b[j].y - b[i].y;
if(dy - d >= eps) break;
d = min(d, sqrt(dx * dx + dy * dy));
}
}
return d;
}
int main (void)
{
while(scanf("%d", &n) && n){
double aa, bb;
for(int i = 0 ; i < n; i++){
scanf("%lf%lf", &aa, &bb);
a[i] = p(aa, bb);
}
sort(a, a + n);
double ans = solve(a, n);
if(ans - 1e4 > eps) printf("INFINITY\n") ;
else printf("%.4f\n", ans);
}
return 0;
}

UVA 10245 The Closest Pair Problem【分治】的更多相关文章

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

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

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

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

  3. UVA 10245 - The Closest Pair Problem

    Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...

  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. $("xxx").attr添加属性的时候不好用

    今天在工作中碰到了使用$(this).attr("selected","selected")为option属性添加默认值时发现时而好用 时而不好用,后经百度发现 ...

  2. General mistakes in parallel computing

    这是2013年写的一篇旧文,放在gegahost.net上面  http://raison.gegahost.net/?p=97 March 11, 2013 General mistakes in ...

  3. EF为什么向我的数据库再次插入已有对象?(ZT)

    最近做了个多对多对实体对象,结果发现每次只要增加一个子实体,就会自动添加一个父实体进去,而不管该父实体是否已经存在. 找了好久,终于找到这篇文章,照文章内容来看,应该是断开连接导致的. 原文地址:ht ...

  4. pickle 两个使用小方法

    def pickle_load(file_path): f = open(file_path,'r+') data = pickle.load(f) f.close() return data     ...

  5. (转)搭建Spring4.x.x开发环境

    http://blog.csdn.net/yerenyuan_pku/article/details/52831306 先去Spring官网下载Spring4.x.x开发包(本人使用的版本是Sprin ...

  6. Linux之用户权限管理

    chmod(更改目录或文件权限) 在linux中,文件的权限分为3中,拥有者,群组,其他人.而chmod则是对权限更改的命令. u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个组,o 表示其 ...

  7. 字符串 || CodeForces 591B Rebranding

    给一字符串,每次操作把字符串中的两种字母交换,问最后交换完的字符串是多少 arr数组记录每个字母最后被替换成了哪个字母 读入字符前面加一空格 scanf(" %c %c", &am ...

  8. Oracle清空数据库中数据表数据的方法

    一.简介最近在项目发版测试的时候,导出dmp的时候不小心把开发库中的一些脏数据导出来了,测试那边导入进去之后一堆不规范的数据,为了不影响测试结果,于是总结了一个快速清空数据库数据表所有数据的方法. 二 ...

  9. No-8.其他命令

    其他命令 目标 查找文件 find 软链接 ln 打包和压缩 tar 软件安装 apt-get 01. 查找文件 find 命令功能非常强大,通常用来在 特定的目录下 搜索 符合条件的文件 序号 命令 ...

  10. mysql 报错Authentication method 'caching_sha2_password' is not supported

    原文地址:https://blog.csdn.net/u011583336/article/details/80999043 之前工作中用的数据库多是ms sqlserver,偶尔用到mysql都是运 ...