/**
最近点对问题,时间复杂度为O(n*logn*logn)
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double INF = 1e20;
const int N = ; struct Point
{
double x;
double y;
}point[N];
int n;
int tmpt[N]; bool cmpxy(const Point& a, const Point& b) //cmpxy这种写法只能和sort结合运用
{
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
} bool cmpy(const int& a, const int& b) //cmpy这种写法只能和sort结合运用
{
return point[a].y < point[b].y;
} double min(double a, double b)
{
return a < b ? a : b;
} double dis(int i, int j)
{
return sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x)
+ (point[i].y-point[j].y)*(point[i].y-point[j].y));
} double Closest_Pair(int left, int right)
{
double d = INF;
if(left==right)
return d;
if(left + == right)
return dis(left, right); //到最后会返回最小两点的距离-------1
int mid = (left+right)>>; //这个骚操作的意思:>>是右移运算符,5>>1 的意思是将5表示为二进制后把末尾的数删去,其最终效果等同于5/2,是用来取降位平均数的
double d1 = Closest_Pair(left,mid); //上接第一步,返回值给了d1,d2,然后进行以下一系列操作--------2
double d2 = Closest_Pair(mid+,right);
d = min(d1,d2);
int i,j,k=;
//分离出宽度为d的区间
for(i = left; i <= right; i++)
{
if(fabs(point[mid].x-point[i].x) <= d) //fabs 求浮点类型的绝对值,与abs有点相似 用 <d 虽然可能扩大复杂度,但是只有如此了
tmpt[k++] = i;
}
sort(tmpt,tmpt+k,cmpy);
//线性扫描
for(i = ; i < k; i++)
{
for(j = i+; j < k && point[tmpt[j]].y-point[tmpt[i]].y<d; j++) //在此步骤做真正的判断,所以虽然前面可能扩大了各种可能性,但到此处都会解决的
{
double d3 = dis(tmpt[i],tmpt[j]);
if(d > d3)
d = d3;
}
}
return d; //上接第二步,在完成操作以后继续返回给上一级调用的函数,也就是返回第二步---------3
} //从倒推来看整个函数的运行过程为1-2-3-2-3-2-3...... int main()
{
while(true)
{
scanf("%d",&n);
if(n==)
break;
for(int i = ; i < n; i++)
scanf("%lf %lf",&point[i].x,&point[i].y);
sort(point,point+n,cmpxy);
printf("%.2lf\n",Closest_Pair(,n-)/); //此处left和right均为下标,2是除在外面的。。。看错了
}
return ;
}

借鉴了大神的经验,并对c++不懂得语法进行了注释。

加一条,结构体名字和定义的结构体类的名字不能相同。

http://blog.csdn.net/lonelycatcher/article/details/7973046

HDOJ1007的更多相关文章

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 给出n个玩具(抽象为点)的坐标 求套圈的半径 要求最多只能套到一个玩具 实际就是要求最近的两个坐标的距离 ...

  2. hdoj1007【几何】【未完待续】

    题意: 在一个平面上有n(1e5)个点,然后求一个圆来包住这些点,求这个圆的最小半径. 思考: 要使一个圆直接包了这些点,没有任何思路..

  3. (模板)hdoj1007(分治求平面最小点对)

    题目链接:https://vjudge.net/problem/HDU-1007 题意:给定n个点,求平面距离最小点对的距离除2. 思路:分治求最小点对,对区间[l,r]递归求[l,mid]和[mid ...

  4. poj3714 Raid(分治求平面最近点对)

    题目链接:https://vjudge.net/problem/POJ-3714 题意:给定两个点集,求最短距离. 思路:在平面最近点对基础上加了个条件,我么不访用f做标记,集合1的f为1,集合2的f ...

随机推荐

  1. bzoj1633 / P2875 [USACO07FEB]牛的词汇The Cow Lexicon

    P2875 [USACO07FEB]牛的词汇The Cow Lexicon 三维dp 它慢,但它好写. 直接根据题意设三个状态: $f[i][j][k]$表示主串扫到第$i$个字母,匹配到第$j$个单 ...

  2. P4336 [SHOI2016]黑暗前的幻想乡

    P4336 [SHOI2016]黑暗前的幻想乡 矩阵树定理(高斯消元+乘法逆元)+容斥 ans=总方案数 -(公司1未参加方案数 ∪ 公司2未参加方案数 ∪ 公司3未参加方案数 ∪ ...... ∪ ...

  3. OpenCV相关网站推荐(Informative websites related to OpenCV)

    原文来自:http://answers.opencv.org/question/69691/informative-websites-related-to-opencv/ i think it wil ...

  4. STM32系统时钟为什么没有定义呢

    对于使用3.5版本库开发的STM32学习者 有时候不清楚为什么没有时钟定义 那么我们就简单的讲解下吧: 1,函数从启动文件开始运行(汇编文件) 2,若是hd.s 请看151行LDR     R0, = ...

  5. SpringBoot中的Quartz应用

    Spring自带定时器任务: code: import org.springframework.beans.factory.annotation.Configurable; import org.sp ...

  6. 2、extract-text-webpack-plugin提取Sass编译的Css

    cnpm install css-loader --save-dev    //css-loader 是将css打包进js cnpm install style-loader --save-dev   ...

  7. Ansible 操作windows

      1.主控端安装ansible         1) pip install ansible 2.主控端安装相关的包 pip install http://github.com/diyan/pywi ...

  8. NYOJ 116 士兵杀敌(二)(二叉索引树)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=116 题意: 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的 ...

  9. Linux批量更改文件后缀-转载

    一.rename解决 1.  Ubuntu系统下 rename 's//.c//.h/'  ./* 把当前目录下的后缀名为.c的文件更改为.h的文件 2.  CentOS5.5系统下 rename . ...

  10. Java中的this

    首先this作为关键字其实是随着对象的创建而产生的,当我们调用对象的一个方法的时候: 例如: A a = new A(); a.f(1)  其实我们可以理解为a.f(a,1) 编译器默默的把所操作的对 ...