Hdoj 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 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
思路
最小点对算法:
- 只有2个点:就返回这2个点的距离
- 只有3个点:就返回两两组成中最短的距离
- 大于3个点:采用分治,步骤如下:
- 根据横坐标x对所有的店进行升序排列
- 找出中心线L,将点集划分为左右2部分\(SL,SR\)
- 递归分治解决找出\(d = min(dL,dR)\),表示\(SL,SR\)中的最近点对
- 将处于\([L-d,L+d]\)中的点按照y值升序排列,不断更新最近点对的距离(如果最近点对的情况是一个在\(SL\),一个在\(SR\)里面,肯定不会超过这个边界)
代码
#include<bits/stdc++.h>
using namespace std;
struct node
{
double x;
double y;
}a[100010],b[100010];
bool cmpx(node a, node b)
{
return a.x < b.x;
}
bool cmpy(node a, node b)
{
return a.y < b.y;
}
double dis(node a, node b)
{
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}
double binaryCal(int l, int r, node* a)
{
if(r-l == 1) //只有2个点的情况
{
return dis(a[l], a[r]);
}
if(r-l == 2) //有3个点的情况
{
double tmp1 = dis(a[l],a[l+1]);
double tmp2 = dis(a[l+1],a[r]);
double tmp3 = dis(a[l],a[r]);
return min(tmp1, min(tmp2,tmp3));
}
int mid = (l+r)/2;
double min_d = min(binaryCal(l,mid,a), binaryCal(mid+1,r,a));
double sqrt_min_d = sqrt(min_d);
int pos = 0;
for(int i=l;i<=r;i++)
{
if(a[i].x < a[mid].x + sqrt_min_d && a[i].x > a[mid].x - sqrt_min_d)
b[++pos] = a[i];
}//将位于[L-d,L+d]范围的点保存到b数组里面
sort(b+1,b+1+pos,cmpy); //按照y值进行排序
for(int i=1;i<=pos;i++)
for(int j=i+1;j<=pos;j++)
{
if(b[j].y - b[i].y > sqrt_min_d)
break;
min_d = min(min_d,dis(b[i],b[j]));
}
return min_d;
}
int main()
{
int N;
while(scanf("%d",&N)!=EOF)
{
if(N==0) break;
for(int i=1;i<=N;i++)
scanf("%lf%lf",&a[i].x, &a[i].y);
double ans = 0.0;
sort(a+1,a+1+N,cmpx);
ans = binaryCal(1,N,a);
printf("%.2lf\n",sqrt(ans)/2); //最后再处理开平方问题
}
return 0;
}
Hdoj 1007 Quoit Design 题解的更多相关文章
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- hdu 1007 Quoit Design 题解
原题地址 题目大意 查询平面内最近点对的距离,输出距离的一半. 暴力做法 枚举每一个点对的距离直接判断,时间复杂度是 $ O(n^2) $,对于这题来说会超时. 那么我们考虑去优化这一个过程,我们在求 ...
- HDU 1007 Quoit Design(经典最近点对问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- HDU 1007 Quoit Design(二分+浮点数精度控制)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 杭电OJ——1007 Quoit Design(最近点对问题)
Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...
- poj 1007 Quoit Design(分治)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu 1007 Quoit Design (最近点对问题)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1007 Quoit Design【计算几何/分治/最近点对】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1007 Quoit Design 分治求最近点对
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- this is incompatible with sql_mode=only_full_group_by
mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by - Jim_.NET - 博客园 http://www.c ...
- Oracle 内存参数调优设置
Oracle 数据库系统中起到调节作用的参数叫初始化参数,数据库管理员根据实际情况需要适当调整这些 初始化参数以优化Oracle系统. 1 主要系统参数调优介绍 2 系统内存参数的分配 2.1 Ora ...
- java.util (Collection接口和Map接口)
1:Collection和Map接口的几个主要继承和实现类 1.1 Collection接口 Collection是最基本的集合接口,一个Collection代表一 ...
- linux关闭触摸板
关闭触摸板 sudo modprobe -r psmouse 如果打开触摸板就是: sudo modprobe psmouse
- 谷歌浏览器报错 Active resource loading counts reached to a per-frame
Active resource loading counts reached to a per-frame limit while the tab is in background. Network ...
- Learning to Rank(转)
https://blog.csdn.net/kunlong0909/article/details/16805889 Table of Contents 1 前言 2 LTR流程 3 训练数据的获取4 ...
- css居中小技巧
一.行内元素-水平居中 在父元素的样式中添加: text-align:center; 二.定宽块级元素-水平居中 所谓定宽块级元素指块级元素的宽度指定,而不是默认的100%,否则此方法无效: 代码: ...
- 【Java】Android EditText开发的一个容易忽略的坑
这几天接手做一个远程控制Android application,安卓前台的EditText用来输入ip地址.端口等信息,发现EditText的使用存在着巨坑一个! 我在网上找了半天,发现仅仅有人提出这 ...
- M3U8文件
M3U本质上说不是音频文件,它是音频文件的列表文件,是纯文本文件.你下载下来打开它,播放软件并不是播放它,而是根据它的记录找到网络地址进行在线播放. M3U文件的大小很小,也就是因为它里面没有任何音频 ...
- h5 打开 app
目前只支持在浏览器中打开,如果非浏览器,例如 微信 支付宝 钉钉 第三方 app 中会弹出下载页面 schemeUrl 为 和app 约定url openApp() { /* 小希学生端 aoji ...