UVA 10245 The Closest Pair Problem【分治】
题目链接:
http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269
题意:
求平面最近点对。
分析:
经典问题。
n比较大,直接枚举不可。
与上一道的树分治类似,我们也可以将点按照x坐标分成两类。
假设把所有点按照x坐标分成两类,那么有如下两种情况:
- 点p,q同属于左半边
- 点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【分治】的更多相关文章
- UVa 10245 The Closest Pair Problem (分治)
题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- UVA 10245 - The Closest Pair Problem
Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...
- 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 ...
随机推荐
- Socket是什么呢?中间软件抽象层
代表着网络连接 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用 ...
- Java异常归纳
1.使用Tomcat运行“播报哥架构”出现的两大异常 1.1 监听器异常 详细情况:部署好Maven项目,启动TOMCAT提示如下错误 java.lang.ClassNotFoundExcepti ...
- python基础一 day6 序列操作集合
列表删除:pop([index])有返回值 remove('元素‘)没有返回值 按元素删,元素是什么,就写什么,是数字就写数字,不要加引号,加引号就变成字符串了,没有就报错. 字典删除:pop( ’键 ...
- Linux 常用命令:解压缩
目录 Linux 常用命令:解压缩 说明 tar 涉及参数说明: 压缩 解压 zip压缩 涉及参数说明: uzip解压 涉及参数说明: gzip 涉及参数说明: 压缩率比较 Linux 常用命令:解压 ...
- JavaSE-30 BigDecimal类的使用
问题 Java(其他编程语言也存在类似问题)中浮点数直接进行算术运算会导致精度丢失. 示例代码: System.out.println("1.0 - 0.9 =" + (1.0 - ...
- 前端拖动div 效果
/** * author levi * url http://levi.cg.am */ $(function() { $(document).mousemove(function(e) { if(! ...
- github 新建一个仓库后
每次都记不住命令,记一下,防止找不到 echo "# learn18" >> README.md git init git add README.md git comm ...
- 使用iframe引入公共模块
新建一个公共文件head.html <!DOCTYPE html><html lang="en"><head> <meta charset ...
- ArchLinux 安装笔记
前言 在开始之前,请在心中默念三遍: Arch Linux 是世界上最好的发行版, 我一定能掌握她. 环境 VM ware + UEFI + 500G 虚拟磁盘 + 2G 内存 + 桥接网络 下载镜像 ...
- 条款36:绝不重新定义继承而来的non-virtual函数(Never redefine an inherited non-virtual function)
NOTE: 1.绝对不要重新定义继承而来的non-virtual函数.