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 ...
随机推荐
- RecyclerView 缓存机制学习笔记2
RecyclerView 初始化所有的视图后,调用 去缓存(StaggeredGridLayoutManager), 而不是初始化一次缓存一次 存储后系统又会去调用tryGetViewHolderFo ...
- 更新centos系统的python版本
因今天安装一程序需要用到python高版本,所以升级来系统自带的python. 先查询下系统的python版本是多少. #python -V 显示出来的是2.4.3,太老了,现在升级到比较稳定的版本3 ...
- SQLite -创建表
SQLite -创建表 SQLite CREATE TABLE语句用于创建一个新表在任何给定的数据库.创建一个基本表包括表命名和定义其列,每列的数据类型 语法: CREATE TABLE语句的基本语法 ...
- 在Oracle用SQL处理以 System.currentTimeMillis
有時為了系統的需求會紀錄到毫秒(Millisecond),我們會接將得到的值寫入db,但是如果要用SQL 做時間範圍的搜尋,有以下做法( systemdate欄位存放System.currentTim ...
- docker 深入理解之cgroups
cgroups 资源限制 cgroups 是什么 cgroups 最初名为process container,有Google工程师Paul Menage和Rohit Seth于 2006 年提出,后由 ...
- 查看cuda版本和cudann
nvcc -V 没有找到直接查询cudann版本的命令,但发现cudann装在 /usr/local/cuda/lib64/目录下,libcudnn.so就是相应版本
- ubuntu 18.04 start myproject
#!/bin/bash now=$(date +%Y%m%d) cmd='/home/hu/go/src/github.com/coredns/coredns/coreserver -conf /ho ...
- kubeadm1.14.1 安装Metrics Server
Metrics API 介绍Metrics-Server之前,必须要提一下Metrics API的概念 Metrics API相比于之前的监控采集方式(hepaster)是一种新的思路,官方希望核心指 ...
- mongdb查询数据并且返回数据条数
var totall; var a = db.db("Magiccat").collection("jishi_content").find().count({ ...
- nodejs实现网站数据的爬取
// 引入https模块,由于我们爬取的网站采用的是https协议 const https = require('https'); // 引入cheerio模块,使用这个模块可以将爬取的网页源代码进行 ...