题意是求出所给各点中最近点对的距离的一半(背景忽略)。

用分治的思想,先根据各点的横坐标进行排序,以中间的点为界,分别求出左边点集的最小距离和右边点集的最小距离,然后开始合并,分别求左右点集中各点与中间点的距离,从这些距离与点集中的最小距离比较,求得最小距离,此处可按纵坐标排序,将纵坐标距离已经大于之前最小距离的部分都剪枝。

代码如下:

 #include <bits/stdc++.h>
using namespace std;
int n,a[];
struct point
{
double x,y;
}p[];
bool cmpx(point a,point b)
{
return a.x < b.x;
}
bool cmpy(int a,int b)
{
return p[a].y < p[b].y;
}
double dis(point a,point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
}
double min(double a,double b,double c)
{
if(a>b) return b>c?c:b;
return a>c?c:a;
}
double fin(int from,int to)
{
if(from+ == to ) return dis(p[from],p[to]);
if(from+ == to ) return min(dis(p[from],p[from+]),dis(p[from],p[to]),dis(p[from+],p[to]));
int mid = (from+to)>>;
double ans = min(fin(from,mid),fin(mid+,to));
int cnt = ;
for(int i = from; i <= to; i++)
if(abs(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;
ans = min(ans,dis(p[a[i]],p[a[j]]));
}
return ans;
}
int main()
{
while(scanf("%d",&n)&&n)
{
for(int i = ; i < n; i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
sort(p,p+n,cmpx);
printf("%.2lf\n",fin(,n-)/);
}
return ;
}

但是呢,开始时本人并不是这么写的,而是求了所有点中最小的横坐标和纵坐标,然后以此为参照点,分别求其他各点到参照点的距离,以距离排序,再求出相邻两点距离的最小值。这么写是上面写法的用时一半左右,尽管 AC 了,但是这么写是不对的......

如图所示,图中的点 1 和点 2 距离比点 1 和点 3 的距离更近,但是第二种方法则是用点 1 和点 3距离与点 3 和点 2 距离中求较小值。(题目的测试数据中可能没有这样的数据吧......)

第二种方法的代码如下:

 #include <bits/stdc++.h>
using namespace std;
int n;
struct point
{
double x,y,dis;
}st,p[];
bool cmp(point a,point b)
{
if(a.dis!=b.dis) return a.dis < b.dis;
return a.x<b.x;
}
double dist(point a,point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
}
int main()
{
double sml;
while(scanf("%d",&n)&&n)
{
st.x = st.y = 1000000.0;
sml = 1000000.0;
for(int i = ; i < n; i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
if(p[i].x < st.x) st.x = p[i].x;
if(p[i].y < st.y) st.y = p[i].y;
}
for(int i = ; i < n; i++)
p[i].dis = dist(p[i],st);
sort(p,p+n,cmp);
for(int i = ; i < n; i++)
if(dist(p[i],p[i-])<sml) sml = dist(p[i],p[i-]);
printf("%.2lf\n",sml/);
}
return ;
}

HDU 1007(套圈 最近点对距离)的更多相关文章

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

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

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

    题意: 给出平面上的n个点,问任意点对之间的最短距离是多少? 思路: 先将所有点按照x坐标排序,用二分法将n个点一分为二个部分,递归下去直到剩下两或一个点.对于一个部分,左右部分的答案分别都知道,那么 ...

  3. Quoit Design (HDU 1007)平面的最近点对

    题目大意:给定平面上的 n 个点,求距离最近的两个点的距离的一半. n <= 10^5.   晕乎乎的度过了一上午... 总之来学习下分治吧233 分治就是把大问题拆成小问题,然后根据对小问题处 ...

  4. HDU 1007 平面上最近点对 分治

    思路: 分治 套路题 //By SiriusRen #include <cmath> #include <cstdio> #include <algorithm> ...

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

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

  6. HDU 1007 Quoit Design

    传送门 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  7. 【HDU 1007】 Quoit Design

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1007 [算法] 答案为平面最近点对距离除以2 [代码] #include <algorith ...

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

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

  9. UVA10054-The Necklace(无向图欧拉回路——套圈算法)

    Problem UVA10054-The Necklace Time Limit: 3000 mSec Problem Description Input The input contains T t ...

随机推荐

  1. MySQL 报错 1055

    具体报错 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'exer.student.sid' w ...

  2. 【HDU - 4342】History repeat itself(数学)

    BUPT2017 wintertraining(15) #8C 题意 求第n(n<2^32)个非完全平方数m,以及\(\sum_{i=1}^m{\lfloor\sqrt i\rfloor}\) ...

  3. 脚本监控web服务器,工作不正常发邮件提醒

    背景介绍公司有多个web网站,没有配置监控服务,每天都需要定时检查服务器是否工作正常.低效耗时. 代码片段 #!/bin/bash # Author Jerry.huang (Email:Jerry. ...

  4. html内嵌框架

    html内嵌框架 <iframe>标签会创建包含另外一个html文件的内联框架(即行内框架),src属性来定义另一个html文件的引用地址,frameborder属性定义边框,scroll ...

  5. IP地址等价类测试用例

    下面是一个比较完善的设计方案,这个方案中,首先把IP地址分成有效可用的IP地址和有效但不可用的IP地址两个等价类:其中有效可用的IP地址中包括IP地址的A,B,C三类地址,有效但不可用的IP地址包括D ...

  6. 假如你不小心干掉了系统,你该怎么办?(一次手贱的记录 ~ Ubuntu and Python3.6)

    前言 多年未犯低级错误,今天犯了个不大不小的错误,记录下生活点滴吧 今天早上脑海里想了下,如果电脑挂了我要备份哪些东西?然后中午休息的时候就列了一下,没想到晚上就悲剧了... 这个是中午写的: ## ...

  7. Markdown基础(内含:锚点使用,使用HTML,新页面跳转,目录生成)

    Github样式显示参考:点我 之前说过用word写文章,这次说说Markdown写文章(推荐) 逆天推荐使用VSCode编写 装这个插件写作更方便: 内含:锚点使用,使用HTML,新页面跳转,目录生 ...

  8. webpack入门(七) API in LOADERS

    介绍 loaders允许你用require() 预处理文件(preprocess files)或者加载他们,在其他的构建工具中,loaders就是一种像“任务(tasks)”的东西.他提供了一种处理前 ...

  9. docker镜像操作

    1.获取镜像 docker pull NAME[:TAG] 如果不显式地指定TAG,则默认会选择latest标签,即下载仓库中最新版本的镜像.//获取最新镜像docker pull ubuntu // ...

  10. Electron入门笔记(二)-快速建立hello world

    官方的文档我没有看懂,看了不少别人的博客和文章,终于慢慢看懂了如何快速的建立一个Electron app demo,前一篇文章不是使用官方快速搭建的,而且还出了小问题,所以去撸了一遍quick-sta ...