HDU1007--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 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
Recommend
JGShining
大意:
平面中有n个点,求要使一个固定半径的圆一次只能包围一个点的最大半径
即为求点集中的最近点对
思路:
采用了算法导论33.4节中介绍的分治法求平面最近点对,时间复杂度为:O(nlogn)
代码:
//平面最近点对,使用分治法
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
const double eps = 1e-6;
const int MAXN = 100010;
const double INF = 1e20;
struct Point
{
double x, y;
};
double dist(Point a, Point b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
Point p[MAXN];
Point tmpt[MAXN];
bool cmpxy(Point a, Point b)//排序时的比较函数
{
if (a.x != b.x)return a.x < b.x;
else return a.y < b.y;
}
bool cmpy(Point a, Point b)//按照y值排序
{
return a.y < b.y;
}
double Closest_Pair(int left, int right)
{
double d = INF; if (left == right)return d;
if (left + 1 == right)
return dist(p[left], p[right]);//递归边界 int mid = (left + right) / 2; double d1 = Closest_Pair(left, mid);//分治求两个点集合的最近点对
double d2 = Closest_Pair(mid + 1, right); d = min(d1, d2);
int k = 0;
for (int i = left; i <= right; i++)
{
if (fabs(p[mid].x - p[i].x) <= d)
tmpt[k++] = p[i];
} //tmpt为与中线距离小于等于d的点的集合
sort(tmpt, tmpt + k, cmpy);
for (int i = 0; i < k; i++)
{
for (int j = i + 1; j < k && tmpt[j].y - tmpt[i].y < d; j++)
{
d = min(d, dist(tmpt[i], tmpt[j]));
}
}//合并分治结果
return d;
}
int main()
{
int n;
while (scanf("%d", &n) == 1 && n)
{
for (int i = 0; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
sort(p, p + n, cmpxy);//对p进行预排序
printf("%.2lf\n", Closest_Pair(0, n - 1) / 2);
}
return 0;
}
HDU1007--Quoit Design(平面最近点对)的更多相关文章
- HDU-1007 Quoit Design 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少.. //STATU ...
- HDU1007 Quoit Design掷环游戏
Quoit Design 看懂题意以后发现就是找平面最近点对间距离除以2. 平面上最近点对是经典的分治,我的解析 直接上代码 #include<bits/stdc++.h> using n ...
- Quoit Design(最近点对+分治)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- HDU1007 Quoit Design 【分治】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- (hdu1007)Quoit Design,求最近点对
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- HDU 1007 Quoit Design 平面内最近点对
http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...
- HDOJ-1007 Quoit Design(最近点对问题)
http://acm.hdu.edu.cn/showproblem.php?pid=1007 给出n个玩具(抽象为点)的坐标 求套圈的半径 要求最多只能套到一个玩具 实际就是要求最近的两个坐标的距离 ...
- 【HDOJ】P1007 Quoit Design (最近点对)
题目意思很简单,意思就是求一个图上最近点对. 具体思想就是二分法,这里就不做介绍,相信大家都会明白的,在这里我说明一下如何进行拼合. 具体证明一下为什么只需要检查6个点 首先,假设当前左侧和右侧的最小 ...
- HDU1007.Quoit Design
-- 点我 -- 题目大意 :给你一堆点,求一个最小圆能够覆盖两个点的半径(最近两点距离的一半): 最多100000个点,暴力即O(n^2)会超时,考虑二分,先求左边最短距离dl,右边dr, 和一个点 ...
- HDU 1007 Quoit Design | 平面分治
暂鸽 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #d ...
随机推荐
- C#编码好习惯,献给所有热爱c#的同学
1. 避免将多个类放在一个文件里面. 2. 一个文件应该只有一个命名空间,避免将多个命名空间放在同一个文件里面. 3. 一个文件最好不要超过500行的代码(不包括机器产生的代码). 4. 一个方法的代 ...
- PowerTool(杀毒辅助工具) V4.6 中文免费绿色版
软件名称: PowerTool(杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 968KB图片预览: 软件简 ...
- Process Monitor V2.96 (系统监视工具) 汉化免费绿色版
软件名称: Process Monitor V2.96 (系统监视工具) 汉化免费绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP ...
- 5、sha1加密的一个坑
OC语言写的sha1加密算法,在网上随手可以搜索到(如下便是),但是我不得不说有一些人不责任,没有提醒大家导入必要的系统头文件,从而导致错误 + (NSString *) sha1:(NSString ...
- VC6.0 通过崩溃地址中找到异常代码行
来源:http://blog.csdn.net/mydeardingxiaoli/article/details/20371585 这是从“VC编程经验总结7”中转出来的借花献佛——如何通过崩溃地址找 ...
- Drupal设置首页默认内容
接触Drupal时间不长,记录一下学习点滴~ Drupal首页的内容,默认是取node表的内容展示的,如果想让首页展示自己创表的内容怎么办呢?以Drupal7为例 在这个admin/config/sy ...
- MyBatis中update的使用
当你传入所需要修改的值为一个实体对象时,可能只改动了其中部分的值.那么其他值需要做一个判断是否为空值的操作. XXXmapper.xml <update id="updateMembe ...
- 大学二三事——那些事(1)
虽然另外一个队友早上忽然拉肚子没有办法去了,我个阿骚还是决定出发. 本来以为早点过去签到可以躲过李导,没想到在她上班的路上被她撞见. 坐在早上那班发往周至县的客车的时候,天气忽好忽坏. 从周至 ...
- sf中schedule设定
博客园龄有两年多了,看了一下我发的文章数和最后发布的日期,不禁的心头一怔,已经有一年都没有写更新博客了.突然想起一个句子好像说的是我:间歇性踌躇满志,持续性懒惰等死.最近也看到一位好朋友的qq个性签名 ...
- yield 学习笔记
第三部分(先看) 先讲 iterator 和 iterable 可迭代对象 (Iterable) 是实现了__iter__()方法的对象, 通过调用iter()方法可以获得一个迭代器 (Iterato ...