Quoit Design&&http://acm.hdu.edu.cn/showproblem.php?pid=1007

本文探讨了如何通过分治法解决求最近点对的问题,给出了一种有效的算法实现,并提供了完整的AC代码示例。该算法能够精确计算平面上两点之间的最小距离。

求最近点对,模板题。。。。采用的是分治的思想。http://blog.youkuaiyun.com/smallacmer/article/details/7763407



Problem Description
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.

Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.

Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.

Sample Input
  
2 0 0 1 1 2 1 1 1 1 3 -1.5 0 0 0 0 1.5 0

Sample Output
  
0.71 0.00 0.75
AC代码:

#include<iostream>
#include<string.h>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 100005
using namespace std;
typedef struct
{
	double x;
	double y;
}Node;
Node s[N],s1[N];
bool cmp(Node a,Node b)
{return a.x<b.x;}
bool cmp1(Node a,Node b)
{return a.y<b.y;}
double distan(Node a,Node b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double slove(int le,int ri)
{
	if(le+1==ri) return distan(s[le],s[ri]);
	if(le+2==ri) return min(min(distan(s[le],s[ri]),distan(s[le+1],s[ri])),distan(s[le],s[le+1]));
	int mid=(le+ri)>>1;
	double ans=min(slove(le,mid),slove(mid+1,ri));
	 int cnt=0;
	 for(int i=le;i<=ri;++i)
		 if(s[i].x-s[mid].x<=ans||s[mid].x-s[i].x<=ans)
			 s1[cnt++]=s[i];
	 sort(s1,s1+cnt,cmp1);
	 for(int i=0;i<cnt;++i)
		 for(int j=i+1;j<cnt;++j)
		 {
			 if(s1[j].y-s1[i].y>=ans) break;
			 ans=min(ans,distan(s1[i],s1[j]));
		 }
		 return ans;
}
int main()
{
	int n;
	while(~scanf("%d",&n),n)
	{
		for(int i=0;i<n;++i) scanf("%lf%lf",&s[i].x,&s[i].y);
		sort(s,s+n,cmp);
		printf("%.2lf\n",slove(0,n-1)/2);
	}return 0;
}



Quoit Design”通常指的是一道经典的算法竞赛题目。该问题的核心是在平面上给定一系列的点,需要找出其中距离最近的两个点之间距离的一半,这个距离可以类比为游戏中套圈的半径设计。 ### 解决思路与步骤 1. **排序**:首先将所有点按照 $x$ 坐标进行排序。 2. **分治**:将排序后的点集从中间分成左右两部分,分别递归地在左右两部分中找出最近点对的距离 $d_1$ 和 $d_2$,并取 $d = \min(d_1, d_2)$。 3. **合并**:考虑跨越左右两部分的点对。找出所有 $x$ 坐标距离中间分割线不超过 $d$ 的点,将这些点按照 $y$ 坐标排序。然后遍历这些点,对于每个点,只需要检查它后面与它 $y$ 坐标差值不超过 $d$ 的点对,更新最小距离。 ### 示例代码(Python 实现) ```python import math def distance(p1, p2): return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2) def closest_pair(points): n = len(points) if n <= 3: min_dist = float('inf') for i in range(n): for j in range(i + 1, n): dist = distance(points[i], points[j]) if dist < min_dist: min_dist = dist return min_dist mid = n // 2 mid_point = points[mid] left_points = points[:mid] right_points = points[mid:] d_left = closest_pair(left_points) d_right = closest_pair(right_points) d = min(d_left, d_right) strip = [] for point in points: if abs(point[0] - mid_point[0]) < d: strip.append(point) strip.sort(key=lambda point: point[1]) for i in range(len(strip)): for j in range(i + 1, min(i + 7, len(strip))): dist = distance(strip[i], strip[j]) if dist < d: d = dist return d # 示例使用 points = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] points.sort() result = closest_pair(points) print(result / 2) ``` ### 复杂度分析 - **时间复杂度**:$O(n \log n)$,其中 $n$ 是点的数量。排序操作的时间复杂度是 $O(n \log n)$,分治和合并的过程也是 $O(n \log n)$。 - **空间复杂度**:$O(n)$,主要用于存储点集和递归调用栈。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值