HDU 1007 Quoit Design[最小距离]

题目

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.

题目大意

给出n个点的二维位置,计算出所有点中的最小距离,然后输出其值的一般。

Input

The input consists of several test cases. For each case, the first line contains an integer N ( 2 ≤ N ≤ 100 , 000 ) N (2 \le N \le 100,000) N(2N100,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

分析

题目中给的n值规模在 1 0 5 10^5 105,首先排除通过循环计算任意两点间的距离来获取最小距离的办法,这样的时间复杂度为 O ( n 2 ) O(n^2) O(n2),必然会导致TLE。那么我们需要对算法进行优化。
首先,我们可以按照点的x轴坐标排序,然后按照点数,给双方各划分一半的点,采用递归的办法分化各个点集直到一个集合中的点数小于等于3时,计算出这两个点或者三个点中的最小距离。然后和另一半集合中的最小距离比较较,得到当下的最短距离,根据这个最小距离将集合中x值与划分线x值的差值比最小距离小的点放入一个数组中,因为在合并两个集合时,左右差值大于当前最小距离的不可能是左右更近点。然后对这些点按照y值大小排序,对于任意两点间y值差值大于最小距离的也不可能是更近点,因此只需对一个点附近y值差值在最小距离以内的点进行计算即可。并且由分析可知,这样的点不会超过8个。“田”的模型。此时的算法复杂度为O(n*log n)。

代码

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

struct Point{
	double x;
	double y;
}a[100005];

bool cmp_x(Point x,Point y){
	return x.x<y.x;
}

bool cmp_y(Point x,Point y){
	return x.y<y.y;
}

double dist(Point x,Point y){
	return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}

double Mindist(int left,int right){
	if(right-left+1<=3){//划分到3个点以内
		if(right==left+1)//两个点
			return dist(a[left],a[right]);
		return min(dist(a[left],a[right]),min(dist(a[left],a[left+1]),dist(a[right-1],a[right])));
		//三个点的情况
	}
	int mid = (left+right)/2;//划分中点
	double len = min(Mindist(left,mid),Mindist(mid+1,right));
	int cnt = 0;
	Point tmp[100005];
	for(int i = left;i<=right;++i){
		if(len>=abs(a[i].x-a[mid].x))//mid周围x在最小距离范围的点
			tmp[cnt++] = a[i];
	}
	sort(tmp,tmp+cnt,cmp_y);//按y排序
	for(int i = 0;i<cnt;++i){
		for(int j = i+1;j<cnt;++j){
			if(len<=abs(tmp[i].y-tmp[j].y))//寻找可能的最小点
				break;//y递增,后面的都可以排除
			len = min(len,dist(tmp[i],tmp[j]));
		}
	}
	return len;
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0)
			break;
		for(int i = 0;i<n;++i)
			scanf("%lf %lf",&a[i].x,&a[i].y);
		sort(a,a+n,cmp_x);//按x排序
		double ans = Mindist(0,n-1);
		printf("%.2f\n",ans/2);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

registor11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值