uva 10245 The Closest Pair Problem (暴力+剪枝)

uva 10245 The Closest Pair Problem

题目大意:给出n个点,求出距离最近的两点间的距离。若点与点间的距离都大于10000,输出INFINITY

解题思路:这题的正统做法是分治,偷懒方法是暴力加剪枝。先按x坐标排序,然后fabs(p[i] - p[j]) > ans的点就可以直接跳过了。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct point{
	double x, y;
};
double cal(double x1, double y1, double x2, double y2) {
	return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
point p[10005];
int cmp(point a, point b) {
		return a.x < b.x;
}
int main() {
	int n;
	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, cmp);
		double ans = 0xFFFFFFF;
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (fabs(p[i].x - p[j].x) - ans > 1e-9)	break;
				double temp = cal(p[i].x, p[i].y, p[j].x, p[j].y);
				if (temp - ans < 1e-9) {
					ans = temp;
				}
			}
		}
		if (ans - 10000 < 0) {
			printf("%.4lf\n", ans);
		}
		else printf("INFINITY\n");
	}
	return 0;
}
时间: 2024-10-17 11:03:22

uva 10245 The Closest Pair Problem (暴力+剪枝)的相关文章

UVa 10245 The Closest Pair Problem (分治)

题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一个在右边的,我们可以先求全在左边或右边的最小值,假设是d,那么一个点在左边,一个点在右边,那么横坐标之差肯定小于d,才能替换d,同样的纵坐标也是,并且这样的点并不多,然后就可以先选出来,再枚举. 代码如下: #pragma comment(linker, "/STACK:1024000000,102

UVA - 10245 The Closest Pair Problem

属于Divide-and-Conquer,算法课老师有讲到,就找个题目试试,思想就是不断的二分...考虑合并时的处理..不解释 //============================================================================ // Name : uva10245.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Uva10

uva10245 - The Closest Pair Problem(暴力+剪枝)

题目:uva10245 - The Closest Pair Problem(暴力+剪枝) 题目大意:给出N个点,求这些点中最小的两点距离. 解题思路:把这些点中两两之间的距离都算出来,这样的复杂度是O(n^2),会超时,所以加了一个减枝. 先将所有的点按照x坐标排序.然后在计算的过程中,如果发现要计算的这两点的X坐标之差的绝对值已经大于等于当前的最小值,那么说明后面的点计算距离一定比这个最小值要大. 这题的正解貌似是分治法,可惜没看懂. 代码: #include <stdio.h> #inc

uva10245-The Closest Pair Problem(平面上的点分治)

解析:平面上的点分治,先递归得到左右子区间的最小值d,再处理改区间,肯定不会考虑哪些距离已经大于d的点对,对y坐标归并排序,然后从小到大开始枚举更新d,对于某个点,x轴方向只用考虑[x-d,x+d](x是分的中轴线),y轴方向只用考虑[y-d,y](y是这个点的y值),因为d值一直在变小,所以这个矩形包含的点数很少. 代码 #include<cstdio> #include<cstring> #include<string> #include<vector>

HDU 6697 Closest Pair of Segments (计算几何 暴力)

2019 杭电多校 10 1007 题目链接:HDU 6697 比赛链接:2019 Multi-University Training Contest 10 Problem Description The closest pair of points problem is a well-known problem of computational geometry. In this problem, you are given \(n\) points in the Euclidean plan

UVA 152-Tree&#39;s a Crowd(暴力求解三维坐标求最短距离)

Tree's a Crowd Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Tree's a Crowd  Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new

uva 639 Don&#39;t Get Rooked (暴力回溯 )

uva 639 Don't Get Rooked In chess, the rook is a piece that can move any number of squares vertically or horizontally. In this problem we will consider small chess boards (at most 44) that can also contain walls through which rooks cannot move. The g

UVA 100 The 3n + 1 problem(超级大水题)

The 3n + 1 problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you

uva:10487 - Closest Sums(二分查找)

题目:10487 - Closest Sums 题目大意:给出一组数据,再给出m个查询的数字.要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和. 解题思路:二分查找,这样找到的话,就输出查询的数值,但是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据,所以要维护最靠近的要查询数的数值. 代码: #include <stdio.h> #include <algorithm> #include <stdlib.h> using