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 ...
随机推荐
- Mac定制终端:iTerm2 + zsh + powerline
原始界面: 配置后的界面: 安装iTerm2 可以直接去官网下载:https://www.iterm2.com/ 下载后直接安装即可 安装主题 所有主题:https://iterm2color ...
- leetcode 155. 最小栈(c++)
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中.pop() -- 删除栈顶的元素.top() -- 获取栈顶元素.get ...
- C# 中定义斜杠 \
\ 是转义符 如 \’ 单引号 \” 双引号 \\ 反斜杠 \0 空 \a 警告(产生峰鸣) \b 退格 \f 换页 \n 换行 \r 回车 ...
- vc code 一个非常不错的插件
https://marketplace.visualstudio.com/items?itemName=CoenraadS.bracket-pair-colorizer 这个是地址,前提下是安装了vc ...
- 初识内存分配ByteBuf
初识ByteBuf: ByteBuf 是Netty 整个结构里面最为底层的模块,主要负责把数据从底层IO 里面读到ByteBuf,然后传递给应用程序,应用程序处理完成之后再把数据封装成ByteBuf ...
- 记录XorDDos木马清理步骤
1.检查 查看定时任务文件发现有两个异常定时任务 [root@manage ~]# cat /etc/crontab # * * * * * user-name command to be execu ...
- centos下Linux C语言MD5的使用
在Linux C变成中用到MD5加密会使用到openssl库,下面给出的是一个简单的小Demo: #include <stdio.h> #include <openssl/md5.h ...
- 安装Kubernetes V1.16.2
安装Kubernetes V1.16.2 准备硬件环境 利用VirtualBox准备两台Linux虚拟机(K8S集群2台起步),系统用CentOS(我用的是的CentOS-7-x86_64-DVD-1 ...
- [暑假集训Day2T1]种树
标算是贪心,我写了个差分约束????? 设dist[i]表示1-i号土地种的树的总棵数,考虑以下几种约束条件: 1)dist[y]>=dist[x]+z,即x号土地至y号土地间至少种了z棵树 2 ...
- Numpy Ndarray对象1
标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指 针.这样为了保存一个简单的[1,2,3],需要有3个指针和三 ...