zoj 2107&&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 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

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

参考了模板写的,确实不是很懂

来源:http://blog.csdn.net/cxiaokai/article/details/6661005

参考资料:http://blog.csdn.net/lishuhuakai/article/details/9133961

http://blog.csdn.net/hackbuteer1/article/details/7482232

尚待理解

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <math.h>
 7 #define MAXX 100005
 8 using namespace std;
 9
10
11 struct point
12 {
13     double x;
14     double y;
15 }p[MAXX],p1[MAXX],p2[MAXX];
16
17 bool cmpx(point a,point b)
18 {
19     return a.x < b.x;
20 }
21 bool cmpy(point a,point b)
22 {
23     return a.y < b.y;
24 }
25 double dis(point a,point b)
26 {
27     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
28 }
29 double minn(double a,double b)
30 {
31     return a > b ? b : a;
32 }
33 double closest(int l,int r)
34 {
35     if(l+1 == r)return dis(p1[l],p1[r]);
36     if(l+2 == r)
37         return minn(dis(p1[l],p1[l+1]),minn(dis(p1[l],p1[r]),dis(p1[l+1],p1[r])));
38     int mid=(l+r)>>1;
39     double ans=minn(closest(l,mid),closest(mid+1,r));
40     int cn=0;
41     for(int i=l; i<=r; i++)
42     {
43         if(p1[i].x>=p1[mid].x-ans&&p1[i].x<=p1[mid].x+ans)
44         {
45             p2[cn++]=p1[i];
46         }
47     }
48     sort(p2,p2+cn,cmpy);
49     for(int i=0; i<cn; i++)
50     {
51         for(int j=i+1; j<cn; j++)
52         {
53             if(p2[j].y-p2[i].y>=ans)
54                 break;
55             ans=minn(ans,dis(p2[i],p2[j]));
56         }
57     }
58     return ans;
59 }
60
61 int main()
62 {
63
64     int n;
65     while(scanf("%d",&n)!=EOF&&n)
66     {
67         for(int i=0; i<n; i++)
68         {
69             scanf("%lf%lf",&p[i].x,&p[i].y);
70             p1[i]=p[i];
71         }
72         sort(p1,p1+n,cmpx);
73         double dist=closest(0,n-1);
74         printf("%.2lf\n",dist/2);
75     }
76     return 0;
77 }

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

时间: 2024-08-07 00:17:24

zoj 2107&&hdu 1007最近点对问题的相关文章

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

HDU 1007 最近点对

分治法求最近点对 递归将点不断分成小组,计算最短距离.此时的最短距离仅仅是两点都属两块的某一块(这里的分割点是mid点). 还需要考虑两点分属两块的情况.这时对于选点则把范围缩小到了以mid为中心.再将距离mid点x轴2*mindist范围点考虑在内.在这些点中,再取mid点,留下那些 y的距离不大于middist 的点进行距离计算. PS:刚开始min函数写错了,写成了max,一直tle.使用max则会导致选取的点越来越多.TLE则就不奇怪了. 对于使用递归方法实现分治,其实质使用了小范围内得

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

本来这个算法在笔者电脑里无人问津过一段时间了,但今天正好做HDU 1007见到了这个问题,今天就来把代码分享出来吧! 我们首先将所有点按照坐标x排序一下,再做一条直线l当作"分割线",方便我们递归. 然后,我们就可以把这些点按照x轴的坐标分为左半部分和右半部分.那么最短距离一定在左半部分.右半部分.跨越左右的点对中的一个. 那么你可能会有疑问了:本来最近点对也一定在这三个区域内,这不还是相当于什么都没干吗? 还真不是.我们可以假设通过递归得到了左边最小距离为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): 29344    Accepted Submission(s): 7688 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

zoj 2107 最近点对

题意: 给出n个点,求最近点对的距离/2. 限制: 2 <= n <= 1e5 思路: 点分治 /*zoj 2107 题意: 给出n个点,求最近点对的距离/2. 限制: 2 <= n <= 1e5 思路: 点分治 */ #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; const double INF

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

ZOJ - 2107 Quoit Design [分治]

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17751 题目描述:求最近点对 题目分析:分治,(nlogn): 为什么,第二轮按排序:http://noalgo.info/793.html 代码: //problem: zoj 2107 Quoit Design //author: ACsorry //result: Yes #include<iostream> #include<cstdio>

ZOJ 3048 (HDU 2258) Continuous Same Game (1)

Problem Description Continuous Same Game is a simple game played on a grid of colored blocks. Groups of two or more connected (orthogonally, not diagonally) blocks that are the same color may be removed from the board. When a group of blocks is remov

ZOJ 2334 HDU 1512 Monkey King

题意: 猴子们打架  认识的猴子不会打架  两只猴子打完以后就认识了  A认识B B认识C A也认识C  每次打架由两伙猴子进行  分别选出自己的最高战斗力  在战斗之后两只猴子战斗力减半  给出m次打架  输出打架后这一伙猴子里的最强战斗力 思路: 判断两只猴子是不是一伙的  用到并查集 快速找出一伙猴子中的最强战斗力用到堆  但打完架两伙猴子合并时堆需要nlogn复杂度  因此用左偏树代替堆 代码: #include<cstdio> #include<cstring> #inc