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

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

代码如下:

 #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. windows刷新本机DNS缓存

    ipconfig /flushdns

  2. ZJOI 2019 划水记

    作为一个极其蒟蒻的OIer,虽然没有省选资格但还是去见见世面. ZJOI2019一试是在浙江省镇海中学.听名字就很霸气. 学习OI的最后一年,记录下一些事情,即使最终走到最后也一无所获,也是一段美好的 ...

  3. 每天一个linux命令(02):route命令

    route命令用来显示并设置Linux内核中的网络路由表,route命令设置的路由主要是静态路由.要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现. 在L ...

  4. Spring的核心

    技术书籍这么多,每次好不容易读完一本,但总过不了多久就会遗忘.为了对抗,整理记录和回看,也是实属必要.由此,从这<Spring 实战(第四版)>开始,记录一下知识点,下次再要复习时,能免去 ...

  5. Zabbix监控服务器硬盘状态

    安装Iptables服务: [root@localhost /]# yum install iptables-services [root@localhost /]# vim /etc/sysconf ...

  6. 【洛谷P3455】ZAP-Queries

    题目大意:求 \[\sum\limits_{i=1}^a\sum\limits_{j=1}^b[gcd(i,j)=c]\] 题解:学会了狄利克雷卷积. \[\epsilon=\mu \ast 1\] ...

  7. 加密解密DES之Android、IOS、C#实现

    Android实现 package com.sto.express.utils; import java.security.MessageDigest; import java.security.sp ...

  8. Luogu P3868 [TJOI2009]猜数字

    题目链接 \(Click\) \(Here\) 中国剩余定理的板子.小心取模. #include <bits/stdc++.h> using namespace std; const in ...

  9. Collection 接口

    Collection 接口中的方法 ArrayList implements List List extends Collection 主要方法:toArray(); 集合转数组 clear(); 清 ...

  10. qml: QtCharts模块的使用(基本配置)------<一>

    QtCharts模块可以用于绘制图表: 导入模块: import QtCharts 2.2 例子: import QtQuick 2.0 import QtCharts 2.2 ChartView { ...