UVa 10245 The Closest Pair Problem (分治)
题意:给定 n 个点,求最近两个点的距离。
析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一个在右边的,我们可以先求全在左边或右边的最小值,假设是d,那么一个点在左边,一个点在右边,那么横坐标之差肯定小于d,才能替换d,同样的纵坐标也是,并且这样的点并不多,然后就可以先选出来,再枚举。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} struct Point{
double x, y;
bool operator < (const Point &p) const{
return x < p.x || x == p.x && y < p.y;
}
};
Point a[maxn]; bool cmp(const Point &lhs, const Point &rhs){
return lhs.y < rhs.y;
} double dfs(Point *a, int n){
if(n < 2) return inf;
int m = n / 2;
double mid = a[m].x;
double d = min(dfs(a, m), dfs(a+m, n-m));
vector<Point> y;
for(int i = 0; i < n; ++i)
if(fabs(mid - a[i].x) < d) y.push_back(a[i]); sort(y.begin(), y.end(), cmp);
for(int i = 0; i < y.size(); ++i)
for(int j = i+1; j < y.size(); ++j){
if(fabs(y[i].y-y[j].y) >= d) continue;
double xx = y[i].x - y[j].x;
double yy = y[i].y - y[j].y;
d = min(d, sqrt(xx*xx + yy*yy));
}
return d;
} int main(){
while(scanf("%d", &n) == 1 && n){
for(int i = 0; i < n; ++i) scanf("%lf %lf", &a[i].x, &a[i].y);
sort(a, a + n);
double ans = dfs(a, n);
if(ans >= 10000.0) printf("INFINITY\n");
else printf("%.4f\n", ans);
}
return 0;
}
UVa 10245 The Closest Pair Problem (分治)的更多相关文章
- 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【分治】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269 题意: 求平面最近点对. 分析: 经典问题. 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 ...
随机推荐
- [樹莓派]用mkusb来制作U盘启动安装Ubuntu 15.04
之前實踐過這文章的描述,還可以成功:http://www.linuxdiyf.com/linux/12719.html,轉記錄餘下: 官方英文文档,教你在Ubuntu 15.04下使用mkusb来制作 ...
- Python 函数 -slice()
功能: slice() 函数实现切片对象,主要用在切片操作函数里的参数传递.返回一个切片对象. 语法: class slice(stop) class slice(start, stop[, step ...
- 不用jq的异步数据获取
function LoadData(url, sign) { var message = ""; if (sign == " ...
- PAT1018——最短路加DFS
http://pat.zju.edu.cn/contests/pat-a-practise/1018 在杭州各个点,有很多自助自行车的点,最大容纳点为CMAX,但比较适合的情况是CMAX/2, 现在从 ...
- 阿里Java开发规范记录(一)
近日,从网上下载了阿里云栖社区发布的<阿里巴巴Java开发手册(正式版)>v1.1.0版.从编程以来,一直苦于没有相关的.全面的.靠谱的规范可以参考,有了这手册,日后编程也算是找到了依 ...
- 求n对括号的合法组合
一道经典的面试题,求n对括号有多少种合法的组合. 抽象为2n个位置,每个位置可以有2种取值,总共有2^2n个组合,附加约束条件是要符合括号的语法,用来剪枝. 括号语法的合法性条件: 初始化左括号和右括 ...
- Py修行路 python基础 (二十二)异常处理
异常处理 一.错误和异常 程序中难免出现错误,而错误分为两种:语言异常和逻辑异常 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) for i in range ...
- Python3 持久化pickle模块
pickle提供了一个简单的持久化功能.可以将对象以文件的形式存放在磁盘上. 1.pickle.dump(obj, file[, protocol]) 序列化对象,并将结果数据流写入到文件对象中.参数 ...
- Deep Learning 学习笔记(5):Regularization 规则化
过拟合(overfitting): 实际操作过程中,无论是线性回归还是逻辑回归,其假设函数h(x)都是人为设定的(尽管可以通过实验选择最优). 这样子就可能出线“欠拟合”或者“过拟合”现象. 所谓过拟 ...
- 每天一个Linux命令 - 【groupadd】
[命令]:grouadd [语法]:groupadd [选项] [参数] [功能介绍]:groupadd 命令勇于创建新的工作组,新工作组的信息将被添加的系统文件中. [选项说明]: -g < ...