杭电oj 1007

先看题目

Quoit Design

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


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
 
题目的大概意思就是他会给一些点的坐标,然后你需要求出这样一个最大的半径的圆使它最多能包含一个点,最多包含一个点也就是不能包含两个点,若包含两个点也就是圆的直径必须大于这两个点的距离,这样我们就不难得到这道题就是让我们求给定点的最小距离,若我们取的半径等于这个最小距离的一半,那么我们就不能包含两个点,而这个半径也是最大的。
题目分析完毕,我们需要求给定点集的最小距离。
求最小距离我们可以把每两个点的距离都求出来,然后求出其中的最小值,这样显示是可以求出最小距离,这样的复杂度是O(n^2)。一般这样做在oj中都是会超时的,所以我们需要找到一种小于O(n^2)复杂度的算法。
我使用的是分治法,时间复杂度O(nlognlogn)。这里先介绍思想
1.把点集分成两部分,对每部分分别求它的最小距离。
2.对每部分分别递归求最小值。
3.已知两部分的最小值,求出总体的最小值。
3是难点,因为就算已知两部分的最小值,也很难求出整体的最小值。
但是知道了每部分的最小值,我们就只需要考虑很少的点就可以的,
因为已知两部分最小距离,整体的最小距离有三种情况,1.第一部分的最小距离,2.第二部分的最小距离,3.一个点在第一部分中,另一个点在第二部分中。
1和2我们已经通过分治求出,3是需要我们考虑的。
求3的情况可以参考这篇博客
下面贴出我得代码
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#define DBL_MAX 1.7976931348623158e+308
using namespace std;
typedef struct Point
{
	double x;
	double y;
};
Point point[100100];
int number;

bool cmp(const Point &a,const Point &b)
{
	if(a.y<b.y)
		return true;
	else 
		return false;
}
double dist(int a,int b)
{
	return sqrt((point[a].x-point[b].x)*(point[a].x-point[b].x)+(point[a].y-point[b].y)*(point[a].y-point[b].y));
}

double divide(int left,int right)
{
	int start,end;
	int i,j;
	int flag;
	double min;
	if(left==right)
		return DBL_MAX;
	if(left+1==right)
		return dist(left,right);
	int middle=(left+right)/2;
	double leftLength=divide(left,middle);
	double rightLength=divide(middle+1,right);
	if(leftLength>rightLength)
		min=rightLength;
	else
		min=leftLength;
	flag=0;
	for(i=left;i<=right;i++)
	{
		if(flag==0&&fabs(point[middle].y-point[i].y)<min)
		{
			start=i;
			flag=1;	
		}
		else if(flag==1&&fabs(point[middle].y-point[i].y)>=min)
		{
			end=i;
			break;
		}
		if(i==right)
		{
			end=right;
		}
	}
	if(flag==0)
	{
		return min;	
	}		
	else
	{
		for(i=start;i<=end;i++)
		{
			for(j=i+1;j<=end;j++)
			{
				if((point[i].x-point[j].x)<min&&dist(i,j)<min)
					min=dist(i,j);
			}
		}
	}
	return min;
}


int main()
{

	int i;
	while(cin>>number&&number)
	{
		for(i=0;i<number;i++)
			cin>>point[i].x>>point[i].y;
		sort(point,point+number,cmp);
		cout<<setprecision(2) <<std::fixed<<divide(0,number-1)/2.0<<endl;
	}
	return 0;
	
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值