hdu 1007 求最近点对

                                                         Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23284    Accepted Submission(s): 6055

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

20 01 121 11 13-1.5 00 00 1.50

Sample Output

0.710.000.75

 

Author

CHEN, Yue

 

Source

ZJCPC2004

 

Recommend

JGShining

先说下题意,很简单,给n个点的坐标,求距离最近的一对点之间距离的一半。

第一行是一个数n表示有n个点,接下来n行是n个点的x坐标和y坐标。实数。

我的代码1400+ms,膜拜3xian400ms牛代码。。。

这个题目其实就是求最近点对的距离。《算法导论》上有详细讲解,王晓东的书上也有代码。主要思想就是分治。先把n个点按x坐标排序,然后求左边n/2个和右边n/2个的最近距离,最后合并。

合并要重点说一下,比较麻烦。

首先,假设点是n个,编号为1n。我们要分治求,则找一个中间的编号m,先求出1m点的最近距离设为d1,还有m+1n的最近距离设为d2。这里的点需要按x坐标的顺序排好,并且假设这些点中,没有2点在同一个位置。(若有,则直接最小距离为0了)。

然后,令dd1, d2中较小的那个点。如果说最近点对中的两点都在1-m集合中,或者m+1n集合中,则d就是最小距离了。但是还有可能的是最近点对中的两点分属这两个集合,所以我们必须先检测一下这种情况是否会存在,若存在,则把这个最近点对的距离记录下来,去更新d。这样我们就可以得道最小的距离d了。

关键是要去检测最近点对,理论上每个点都要和对面集合的点匹配一次,那效率还是不能满足我们的要求。所以这里要优化。怎么优化呢?考虑一下,假如以我们所选的分割点m为界,如果某一点的横坐标到点m的横坐标的绝对值超过d1并且超过d2,那么这个点到m点的距离必然超过d1d2中的小者,所以这个点到对方集合的任意点的距离必然不是所有点中最小的。如果不理解就看下面这个图。


所以我们先把在m为界左右一个范围内的点全部筛选出来,放到一个集合里。

筛选好以后,当然可以把这些点两两求距离去更新d了,不过这样还是很慢,万一满足条件的点很多呢。这里还得继续优化。首先把这些点按y坐标排序。假设排序好以后有p个点,编号为1p。那么我们用1号去和2p号的点求一下距离,然后2号和3p号的点求一下距离。。。还没完,因为这样比,求的次数还是O(p^2),所以其实和没优化没区别。那为什么要这样做呢?这里我们看一下下面这个图。


假设有一个点q,坐标是xq, yq。可以证明在以q为底边中点,长为2d,宽为d的矩形区域内不会有超过6个点。具体证明过程可以参看算法导论。利用这个结论我们就可以来继续优化比较的过程了。刚刚我们是用用1号点去和2p号的点求一下距离,我们知道以1号点构造图中矩形内,不会有超过6个点存在。但是我们又不能直接从1号求到6号,因为这里的p个点是按y坐标排序而不是按距离排序的,有可能在y坐标上,前10个点距离1号点都很近,但是前6个点的x坐标很远,而第10个点的x坐标和1号点的x坐标很进,这样第10个点到1号点的距离反而更近。
那么我们怎么利用这个结论呢?应该这样,假设1号点和2p号点比较,由于y坐标排序的缘故,假设第p个点的y坐标距离1号点的y坐标大于当前能求出的最小值,那么这点以及这点后的所有点距离1号点的距离必然大于当前已经获得的最小值,所以直接不用比较后面的了。
又因为满足比较条件的点很少,不会超过6个,所以这里可以看成O(1)的效率。那么整个算法的效率大概是在O(nlogn),非常快!

但是别忘记我们前面有个对y排序的步骤。这个步骤还需要耗费时间。为了获得高效率的算法,我们只能在一开始的时候直接对y进行排序并且在程序运行期间不改变这个集合。这里预排序的时间是O(nlogn),关于怎么维护点y坐标有序和x坐标有序,就看程序吧。

<span style="font-size:12px;">#include<stdio.h>
#include<algorithm>
#include<math.h>
#define inf 0x3fffffff
using namespace std;
#define N 100005
struct node
{
	double x,y;
}a[N],dis[N];
double min(double a,double b)
{
	return a<b?a:b;
}
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));
}
int jude(double a)
{
	if(fabs(a)<=1e-10)
		return 1;
	else if(a<0)
		return 1;
	else
		return 0;
}
double search(int x,int y)
{
	double d;
	int i,j;
	if(x==y)  return inf;
	if(x+1==y)
		return dist(a[x],a[y]);
	int mid=(x+y)>>1;
	d=min(search(x,mid),search(mid,y));
    int k=0;
	for(i=x;i<=mid;i++)
	{
		if(jude(a[i].x-a[mid].x-d)==1)
			dis[k++]=a[i];
	}
	for(i=mid+1;i<=y;i++)
	{
		if(jude(a[i].x-a[mid].x-d)==1)
			dis[k++]=a[i];
	}
	sort(dis,dis+k,cmpy);
    for(i=0;i<k;i++)
	{
		for(j=1;j<=6&&i+j<k;j++)
			d=min(d,dist(dis[i],dis[i+j]));
	}
	return d;
}
int main()
{
	double ans;
	int i,n;
	while(scanf("%d",&n),n!=0)
	{
		for(i=0;i<n;i++)
			scanf("%lf%lf",&a[i].x,&a[i].y);
		sort(a,a+n,cmpx);
		ans=search(0,n-1);
		printf("%.2lf\n",ans/2.0);
	}
	return 0;
}
</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值