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. 

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

题目大意:套圈游戏,一个圆圈里只能圈一个玩具,为了使游戏更加有吸引力, 把圆圈的半径做的最大,如果有两个玩具的位置相同,则圈的半径为0,因为一个圈不能有两个玩具。给出n个玩具坐标,输出最大半径

解题思路:平面分治最小点对,再把圆圈半径最大的情况下,因为一个圆圈只能圈住一个玩具,所以圆圈的最大半径就是最小点距的一半

AC代码:

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
double const INF=1e100;
struct node{
	double x,y;
}a[maxn],term[maxn];
bool cmpx(node a,node b)
{
	return a.x<b.x;
} 
bool cmpy(node a,node b)
{
	return a.y<b.y;
}
double dist(node a,node b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double minpairs(int left,int right)
{
	if(left==right)
		return INF;
	if(left+1==right)
	{
		if(a[left].x==a[right].x&&a[left].y==a[right].y)
			return 0;//位置相同返回0 
		return dist(a[left],a[right]);
	}
	int mid=(left+right)/2;
	double d1=minpairs(left,mid);
	double d2=minpairs(mid+1,right);
	double dis=min(d1,d2);
	int i,j,k=0;
	for(i=left;i<=right;i++)
	{
		if(fabs(a[i].x-a[mid].x)<=dis)
			term[k++]=a[i];
	}
	sort(term,term+k,cmpy);
	for(i=0;i<k;i++)
	{
		for(j=i+1;j<k;j++)
		{
			if(term[j].y-term[i].y>dis)
				break;
			if(term[i].x==term[j].x&&term[i].y==term[j].y)
				return 0;//位置相同返回0 
			dis=min(dis,dist(term[i],term[j]));
		}
	}
	return dis;
}
int main()
{
	int n;
	while(scanf("%d",&n),n)
	{
		for(int i=0;i<n;i++)
			scanf("%lf%lf",&a[i].x,&a[i].y);
		sort(a,a+n,cmpx);
		double ans=minpairs(0,n-1);
		printf("%.2lf\n",ans/2);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40707370/article/details/85723461
今日推荐