UVA 10245 The Closest Pair Problem(平面最近点对)
来源:互联网 发布:礼券提货系统源码 php 编辑:程序博客网 时间:2024/05/28 15:59
解题思路:
分治法求平面最近点对。
将所有点排序后,最近点对的距离就是下面两者的最小值:
(1) 两点p, q 同属于左半边或右半边时点对(p, q ) 的距离;
(2) 两点p, q属于不同区域时,距离小于d的点对(p, q)的距离。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <vector>#include <algorithm>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;const int MAXN = 10000 + 10;const double eps = 1e-6;int N;typedef pair<double, double> Point;Point A[MAXN];bool compare_y(Point a, Point b){ return a.second < b.second;}double closest_pair(Point *a, int n){ if(n <= 1) return INF; int m = n / 2; double x = a[m].first; double d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<Point> b; for(int i=0;i<n;i++) { if(fabs(a[i].first - x) >= d) continue; for(int j=0;j<b.size();j++) { double dx = a[i].first - b[b.size()-j-1].first; double dy = a[i].second - b[b.size()-j-1].second; if(dy >= d) break; d = min(d, sqrt(dx * dx + dy * dy)); } b.push_back(a[i]); } return d;}void solve(){ sort(A, A + N); double ans = closest_pair(A, N); if(ans >= 10000) cout << "INFINITY" << endl; else printf("%.4lf\n", ans);}int main(){ while(scanf("%d", &N)!=EOF) { if(N == 0) break; for(int i=0;i<N;i++) scanf("%lf%lf", &A[i].first, &A[i].second); solve(); } return 0;}
0 0
- UVA 10245 The Closest Pair Problem(平面最近点对)
- UVa Problem 10245 The Closest Pair Problem (最近点对问题)
- UVA-10245 The Closest Pair Problem(最近点对问题)
- UVa 10245 The Closest Pair Problem (计算几何&最近点对)
- The Closest Pair Problem(UVa 10245) 计算几何+最近点对问题
- 平面最近点对 Finding the closest pair of points
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
- uva10245 - - The Closest Pair Problem(最近点问题)
- UVa 10245 - The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- uva 10245 - The Closest Pair Problem
- [uva] 10245The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- UVA 10245 - The Closest Pair Problem
- uva 10245 The Closest Pair Problem
- uva 10245 The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- Linux 基本常用操作
- HDU:3420 Bus Fair, 3090 Go Home+uva:11729 Commando War(简单贪心)
- 项目思维
- 邮票面值设计
- POJ 2104 K-th Number
- UVA 10245 The Closest Pair Problem(平面最近点对)
- 浅谈break 、continue、return,goto四种语句的区别。
- JavaScript权威指南_129_第15章_脚本化文档_15.6-创建、 插入、删除节点-插入节点
- javascript正则表达式学习笔记之------正则表达式的创建和基本使用
- 博文:永远不再犯的5个编程错误有感
- NOI 2015 DAY1 T2 软件包管理器 树链剖分
- java Hashcode实际应用场景
- 一个猜数字输赢的小游戏
- 黑色星期五