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

给你平面上n个点,让你找最近的2个点的距离的一半

经典的分治问题,我们现将点按照x坐标排序,先处理前一半的答案,再处理后一半的答案,两个取最小ans

现在还没完呢,万一一个点在左半边另一个点在右半边呢?这样我们就要更新答案了.我们注意到这样的点肯定满足到中心的点的距离不超过ans

我们暴力搞出这些点,这些点是有可能来更新ans的,我们是不是要将这些点n^2算距离更新呢?这样显然是超时的

我们将这些点按y坐标排序,我们对于第1个点开始求它与第2~cnt个点的y坐标的差值,一旦这个差值大于ans就不用再去比较后面的点了,我们再从第2个点求它与第3~cnt个点的y坐标差,以此类推

这样及时的break就优化了......

谁信啊!!!!其实这涉及到一个点周围能够更新ans的点最多有6个,否则上面ans就不是答案了

代码如下:

 #include <bits/stdc++.h>

 using namespace std;
const int maxn = ;
struct point
{
double x,y;
}p[maxn];
int a[maxn];
double dis (point q1,point q2)
{
return sqrt((q1.x-q2.x)*(q1.x-q2.x)+(q1.y-q2.y)*(q1.y-q2.y));
}
bool cmpx (point q1,point q2)
{
return q1.x<q2.x;
}
bool cmpy (int q1,int q2)
{
return p[q1].y<p[q2].y;
}
int n;
double calc (int l,int r)
{
if (r==l+)
return dis(p[l],p[r]);
else if (r==l+)
return min(dis(p[l],p[l+]),min(dis(p[l+],p[r]),dis(p[l],p[r])));
else{
int mid = (l+r)/;
double ans = min(calc(l,mid),calc(mid+,r));
int cnt = ;
for (int i=l;i<=r;++i){
if (p[i].x>=p[mid].x-ans&&p[i].x<=p[mid].x+ans)
a[cnt++]=i;
}
sort(a,a+cnt,cmpy);
for (int i=;i<cnt;++i){
for (int j=i+;j<cnt;++j){
if (p[a[j]].y-p[a[i]].y>=ans) break;
else{
ans = min(ans,dis(p[a[i]],p[a[j]]));
}
}
}
return ans;
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d",&n)){
if (n==) break;
for (int i=;i<n;++i){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmpx);
double ans = calc(,n-);
printf("%.2f\n",ans/);
}
return ;
}

精髓就是通过不断二分从指数上将复杂度降下来

hdu 1007 Quoit Design (经典分治 求最近点对)的更多相关文章

  1. hdu 1007 Quoit Design(分治法求最近点对)

    大致题意:给N个点,求最近点对的距离 d :输出:r = d/2. // Time 2093 ms; Memory 1812 K #include<iostream> #include&l ...

  2. HDU 1007:Quoit Design(分治求最近点对)

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:平面上有n个点,问最近的两个点之间的距离的一半是多少. 思路:用分治做.把整体分为左右两个部分,那么 ...

  3. hdu 1007 Quoit Design(分治)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:给出n个点求最短的两点间距离除以2. 题解:简单的分治. 其实分治就和二分很像二分的写df ...

  4. HDU 1007 Quoit Design | 平面分治

    暂鸽 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #d ...

  5. HDU 1007 Quoit Design(经典最近点对问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...

  6. hdu 1007 Quoit Design 分治求最近点对

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. HDU 1007 Quoit Design【计算几何/分治/最近点对】

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDU 1007 Quoit Design(二分+浮点数精度控制)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  9. hdu 1007 Quoit Design (最近点对问题)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. Quartz-第二篇 使用quartz框架定时推送邮件

    1.定时推送邮件,也就是使用定时调度框架触发我们的发邮件动作,发邮件动作,请参考我的这篇随笔.

  2. Shell脚本语言学习总结

    Shell 是一种脚本程序,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了. 编写第一个Shell 程序 在linux命令行执行 [root@iz2zeexr9tk4ckr7dp ...

  3. P4195 【模板】exBSGS/Spoj3105 Mod

    传送门 首先要懂得 $BSGS$,$BSGS$ 可以求出关于 $Y$ 的方程 $X^Y \equiv Z (mod\ mo)$ 的最小解,其中 $gcd(X,Z)=1$ $exBSGS$ 算是 $BS ...

  4. 自定义InputFormat

    回顾: 在上一篇https://www.cnblogs.com/superlsj/p/11857691.html详细介绍了InputFormat的原理和常见的实现类.总结来说,一个InputForma ...

  5. spring(六):spring中AOP的基本使用

    AOP:面向切面编程[底层使用动态代理实现],就是在运行期间动态的将某段代码切入到方法的指定位置进行运行的编程方式 基本使用 使用AOP功能需要引入spring的aop以及aspects相关包 < ...

  6. 164-基于TI DSP TMS320C6455和Altera FPGA EP2S130的Full CameraLink PDS150接口板卡

    一.板卡概述 本板卡由我公司自主研发,板卡采用DSP+FPGA的结构,DSP使用TMS320C6455芯片,FPGA采用ALTERA的高端FPGA芯片Stratix II EP2S系列EP2S130, ...

  7. 二、TortoiseSVN 合并、打分支、合并分支、切换分支

    一.合并 点击Edit conflict来编辑冲突: 在合并后的枝干对应栏中编辑后,Save保存后关闭. 二.TortoiseSVN 打分支.合并分支.切换分支 1.SVN打分支 方式一:先检出,再打 ...

  8. BZOJ-3143/洛谷3232 游走(HNOI2013)概率DP

    题意:给定n个点m条边.每条边的权值还没决定,权值大小为从1到m.从1出发每次等概率选一条出边向下走,直到走到n点停止,路径代价就是边权总和.由你来决定边权来使得上诉路径代价期望值最小. 解法:点这么 ...

  9. mysql 联合表查询从表即使有索引依然ALL的一个原因

    那就是主表和从表的关联字段的编码方式不一样!!! 晕啊,折腾了半天才发现,可能是不知道啥时候mysql更改主体编码方式了,结果导致后来新建的表的关联字段和之前的主表的字段的编码方式不一样 改成一样的编 ...

  10. MySQL MHA之 master_ip_failover.sh脚本

    master_ip_failover.sh脚本是用perl编写的,可以在mha-manager源码包中可以找到,下面给出的结合keepalived进行自动切换的脚本: [root@mha script ...