uva 10245(分治)
来源:互联网 发布:skynet windows 编辑:程序博客网 时间:2024/06/02 00:05
题意:一些点的坐标给出,求出这些点最近的距离,大于10000输出INF...
题解:直接遍历超时。。于是学会了用分治,先把坐标按x的大小升序排列,然后平均分成两部分,递归找出左半边点最小的距离d1,右半边点最小的距离d2,那么很明显,最后最小的距离肯定是,d1 或者 d2 或者 中间的左边某个点和右边某个点的连线d3,这三者中最短的一个。然后开始找跨中间的两点最小距离,只要左边点与中间点的x坐标差或右边点与中间点的x坐标差小于min(d1, d2),就计算与之比较选出最小值。
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;const int N = 10005;struct Point {double x, y;}p[N];int cmpx(Point a, Point b) {if (a.x != b.x)return a.x < b.x;return a.y < b.y;}double count(Point p1, Point p2) {return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));}double solve(int l, int r) {if (l == r)return 100000.0;if (r - l == 1)return count(p[l], p[r]);double temp1 = solve(l, (l + r) / 2);double temp2 = solve((l + r) / 2, r);double minn = temp1 > temp2 ? temp2 : temp1;int mid = (l + r) / 2;for (int i = mid - 1; i >= l && p[mid].x - p[i].x < minn; i--)for (int j = mid + 1; j <= r && p[j].x - p[mid].x < minn; j++) {double temp = count(p[i], p[j]);if (temp < minn)minn = temp;}return minn;}int main() {int n;double mindist;while (scanf("%d", &n) && n) {for (int i = 0; i < n; i++)scanf("%lf%lf", &p[i].x, &p[i].y);sort(p, p + n, cmpx);mindist = solve(0, n - 1);if (mindist > 10000)printf("INFINITY\n");elseprintf("%.4lf\n", mindist);}return 0;}
0 0
- uva 10245(分治)
- uva 10245 最近点对(分治)
- 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)(分治)
- File Fragmentation 贪心+分治 uva
- UVA - 12627 Erratic Expansion(分治)
- An antiarithmetic permutation +uva+分治
- UVA 11990 bit + cdq 分治
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
- UVA 10245 The Closest Pair Problem(传说中的分治法)
- uva - 11129 - An antiarithmetic permutation(分治)
- 【分治】UVa 11129 An antiarithmetic permutation
- The Closest Pair Problem uva+分治
- UVA-11129 - An antiarithmetic permutation (分治)
- uva--11129An antiarithmetic permutation+分治
- lvs、haproxy、nginx 负载均衡的比较分析
- 黑马程序员——多线程
- unicode和字符串之间的转换有两种方式
- LeetCode OJ - Unique Binary Search Trees II
- 【BZOJ】【P3028】【Usaco2013 Jan】【Cow Lineup】【题解】
- uva 10245(分治)
- 从简单工厂模式到抽象工厂模式的思考
- C++中定义自己的头文件
- 统计相同数字的个数
- if语句的简单使用
- leetcode - Insertion Sort List
- 堆 和 栈的 区别(经典)
- 【备忘】LATEX BEAMER初始化
- 建立本地yum安装库