HDU 1007

Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 33903    Accepted Submission(s): 8856

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

Author

CHEN, Yue

Source

ZJCPC2004

这道题正规的做法是分治,不过因为数据极弱的关系,我普通方法就过了。题意:给你N个点的坐标,叫你找出哪两个点距离最近,然后距离除以2就是所求。。

思路:按X升序排,Y升序排,比较两种的结果哪种距离更小。可得解,跑了一千多毫秒。

上代码解析。

#include <stdio.h>
#define min(a,b) a>b?b:a;
#include <math.h>
#include <algorithm>
using namespace std;
struct point
{
	double x,y;  //必须用double 因为必然有小数点
}
f[100005];
bool cmp(point a,point b)
{
	if(fabs(a.x-b.x)<1e-8)  // 如果两个坐标的距离很接近
		return a.y<b.y;  //  按Y的升序排
	return a.x<b.x;//   X也升序。
}
bool cmp1(point a,point b)
{

	if(fabs(a.y-b.y)<1e-8)// 同上
		return a.x<b.x;
	return a.y<b.y;
}
double find(double a,double b,double c,double d)
{
	return sqrt(pow((a-c),2)+pow((b-d),2));//  计算两点之间的距离
}
void solve()
{
    int n,i;
	double x,y;
	while(scanf("%d",&n)&&n)
	{
		for(i=0;i<n;i++)
			scanf("%lf%lf",&f[i].x,&f[i].y);
		sort(f,f+n,cmp);//  第一种排序
		double p=99999999;
		double s;
		for(i=0;i<n-1;i++)
		{
			s=min(p,find(f[i].x,f[i].y,f[i+1].x,f[i+1].y));//找出最小的距离
			p=s;
		}
		sort(f,f+n,cmp1);// 第二种排序
		   	  for(i=0;i<n-1;i++)
			  {
				  s=min(p,find(f[i].x,f[i].y,f[i+1].x,f[i+1].y));
				  p=s;//找出第一种和第二种哪个距离更小。
			  }
			  s/=2;//除以2就是答案
			  printf("%.2lf\n",s);输出
	}
}
int main()
{
	solve();//问题解决
	return 0;
}
时间: 2024-09-22 08:13:40

HDU 1007的相关文章

HDU 1007 Quoit Design (分治)

Quoit Design 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 ri

初学算法-分治法求平面上最近点对(Closest Pair)-HDU 1007

本来这个算法在笔者电脑里无人问津过一段时间了,但今天正好做HDU 1007见到了这个问题,今天就来把代码分享出来吧! 我们首先将所有点按照坐标x排序一下,再做一条直线l当作"分割线",方便我们递归. 然后,我们就可以把这些点按照x轴的坐标分为左半部分和右半部分.那么最短距离一定在左半部分.右半部分.跨越左右的点对中的一个. 那么你可能会有疑问了:本来最近点对也一定在这三个区域内,这不还是相当于什么都没干吗? 还真不是.我们可以假设通过递归得到了左边最小距离为d1,右边最小距离为d2,令

zoj 2107&amp;&amp;hdu 1007最近点对问题

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds      Memory Limit: 32768 KB 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

hdu 1007 Quoit Design 分治求最近点对

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29344    Accepted Submission(s): 7688 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

HDU 1007 [Quoit Design] 分治

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 题目大意:N个点,求最近一对点的距离的一半. 关键思想:最傻瓜式的枚举所有点对的距离找最短 O(n^2),会TLE. 分治可以优化成O(nlogn).二分区间,考虑三种情形:点对的两点都在左区间.两点都在右区间.两点一左一右 前两种情况,可以递归地解出来,分别为d1,d2.第三种,可以依据min(d1,d2)收缩成一条带状区域(勾股定理显然). 然后对带中所有点进行处理,此时又可依据min(d

【HDU 1007】 Quoit Design

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1007 [算法] 答案为平面最近点对距离除以2 [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #inc

hdu 1007 Quoit Design (经典分治 求最近点对)

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 carefull

Quoit Design(hdu 1007)

题意:给n个点的坐标,求距离最近的一对点之间距离的一半.第一行是一个数n表示有n个点,接下来n行是n个点的x坐标和y坐标.实数. /* 最小点距离问题 采用分治算法,假设对于1-n的区间,我们已经求出1~m(m是中点)和m+1~n的结果分别是d1和d2. 那么如果1~n的答案出现在这两个单独的区间内,很明显就是min(d1,d2),否则在两个区间之间产生. 如果直接两重循环枚举两个区间的数会T,所以考虑优化: ①:如果某个点到m的距离大于min(d1,d2),那么不考虑. ②:首先用到一个结论:

HDU 1007 Quoit Design

Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 130 Accepted Submission(s): 72   Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings a

ZOJ 2107 HDU 1007 Quoit Design(最近点对)

最近点对的裸题 利用分治去搞搞即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int N = 100005; struct Point { double x, y; void read() { scanf("%lf%lf", &x, &y); } }; b