hdu 1007 Quoit Design (经典分治 求最近点对)
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.
给你平面上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 (经典分治 求最近点对)的更多相关文章
- hdu 1007 Quoit Design(分治法求最近点对)
大致题意:给N个点,求最近点对的距离 d :输出:r = d/2. // Time 2093 ms; Memory 1812 K #include<iostream> #include&l ...
- HDU 1007:Quoit Design(分治求最近点对)
http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:平面上有n个点,问最近的两个点之间的距离的一半是多少. 思路:用分治做.把整体分为左右两个部分,那么 ...
- hdu 1007 Quoit Design(分治)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:给出n个点求最短的两点间距离除以2. 题解:简单的分治. 其实分治就和二分很像二分的写df ...
- HDU 1007 Quoit Design | 平面分治
暂鸽 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #d ...
- 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)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) To ...
- hdu 1007 Quoit Design (最近点对问题)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- ubuntu 16.4下hadoop配置伪分布式时出现的坑
在ubuntu16.4下spark的单机/伪分布式配置我在此就不在一一赘述,详情请点击如下连接: Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04 我出现问题是 ...
- spring-cloud zuul网关
API Gateway 是随着微服务(Microservice)这个概念一起兴起的一种架构模式,它用于解决微服务过于分散,没有一个统一的出入口进行流量管理的问题. 使用 Zuul 实现 API Gat ...
- jmeter测试https请求
测试https请求时,需要添加证书 目录 1.下载证书 2.导入 3.执行https请求 1.下载证书 在浏览器中打开要测试的https协议的网站,以谷歌为例打开,下载证书到桌面 4.一直点击下一步 ...
- C语言的未初始化的数组的值为什么是随机的
突然想起来前几天同学问我为什么没有初始化的数组的值是随机的,发现这个困惑自己也是存在的,所以自己总结的心得. 1. 首先,并不是所有未初始化的数组的值都是随机的.对于没有初始化的数组,分两种情况: ( ...
- WebService登陆验证四种方式
在这个WEB API横行的时代,讲WEB Service技术却实显得有些过时了,过时的技术并不代表无用武之地,有些地方也还是可以继续用他的,我之所以会讲解WEB Service,源于我最近面试时被问到 ...
- org.w3c.dom。 XML解析 练习
HTML文档 1 import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; ...
- 【ABAP系列】SAP smartforms金额字段产生空格,除去空格的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP smartforms金额 ...
- Java中的类修饰符
资料主要来源于网络(http://60.28.60.3/zy/java-new/zhishidian/chap3/3.htm) 之前每次写小测试程序的时候,总是把一个类放在一个Java文件中,按理说这 ...
- 《JAVA设计模式》之外观模式(Facade)
在阎宏博士的<JAVA与模式>一书中开头是这样描述门面(Facade)模式的: 门面模式是对象的结构模式,外部与一个子系统的通信必须通过一个统一的门面对象进行.门面模式提供一个高层次的接口 ...
- GNU MAKE 笔记
最近在调试OJ, 忙了4天多, 最后的问题是judge模块不能正常工作. judge 模块就是两个C++源文件, 它的工作是 从数据库获取用户提交的源码 测评 将测评结果写到数据库 测评部分是与数据库 ...