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 ...
随机推荐
- 安卓自定义View教程目录
基础篇 安卓自定义View基础 - 坐标系 安卓自定义View基础 - 角度弧度 安卓自定义View基础 - 颜色 进阶篇 安卓自定义View进阶 - 分类和流程 安卓自定义View进阶 - Canv ...
- webapi参数处理get过个参数
// GET api/values/5 [HttpGet("{logInName}/{pwd}/{orgId}")] public LogInOutPut Get(string l ...
- Ubuntu16.04常用操作命令总结ing
查看软件安装目录:whereis 软件名称(如:whereis mysql,where is sqlite3等) 安装软件:apt/apt-get install 软件名称(如:apt/apt-get ...
- 使用正则进行HTML页面属性的替换
使用正则表达式拼接富文本框 package com.goboosoft.common.utils; import org.apache.commons.lang3.StringUtils; impor ...
- 【转载】用Python实现端口映射功能(A/B/C内外网)
转载地址 :http://hutaow.com/blog/2014/09/08/write-tcp-mapping-program-with-python/ 有A,B,C三台计算机,A,B互通,B,C ...
- C-基础:函数返回局部变量
一般的来说,函数是可以返回局部变量的. 局部变量的作用域只在函数内部,在函数返回后,局部变量的内存已经释放了.因此,如果函数返回的是局部变量的值,不涉及地址,程序不会出错.但是如果返回的是局部变量的地 ...
- PHP15 Smarty模板
学习目标 Smarty基本概念 Smarty安装和配置 Smarty模板设计 Smarty流程控制 Smarty基本概念 一种模板引擎,在系统中进行预处理和过滤数据.是主流的PHP模板引擎,此外PHP ...
- SQL比较两表字段和字段类型
一.问题 业务需要把TB_Delete_KYSubProject表数据恢复到TB_KYSubProject,但提示错误,错误原因是两表字段类型存在不一致 insert into [TB_KYSubPr ...
- SQL 语句解决实际问题
在项目开发过程中,遇到数据库的查询问题 一.查询某表字段的信息 select * from syscolumns SELECT object_id('TB_KYChildProject') selec ...
- 数组对象分类个数js
<script type="text/javascript"> $(function(){ var aaa = [ {"task1":"z ...