http://acm.hdu.edu.cn/showproblem.php?pid=1007

直接见代码吧。不过这个是N*logN*logN的

尽管如此,我怎么感觉我的比他们的还快???

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R //typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; double min(double a, double b) { return a < b ? a : b; } struct Point
{
double x;
double y;
};
int numOfPoint;
Point points[MAXN], TempMerge[MAXN];
Point ansPoint1, ansPoint2;
double closest; void initPoints()
{
mem0(points); closest = INF;
for(int i=;i<numOfPoint;i++)
{
scanf("%lf %lf", &points[i].x, &points[i].y);
}
} int cmp_X(Point A, Point B)
{
if(A.x != B.x) return A.x < B.x;
return A.y < B.y;
} int cmp_Y(Point A, Point B)
{
if(A.y != B.y) return A.y < B.y;
return A.x < B.x;
}
#define distance(A, B) sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y))
//double distance(Point &A, Point &B)
//{
// return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
//} double calculateTheClosest(int low, int high)
{
for(int i=low;i<=high;i++)TempMerge[i-low] = points[i];
sort(TempMerge, TempMerge+(high-low+), cmp_Y);
for(int i=;i<high-low;i++)
{
for(int j=i+;j<=i+ && j<=high-low;j++)
{
double calc = distance(TempMerge[i], TempMerge[j]);
if(calc < closest)
{
closest = calc;
ansPoint1 = TempMerge[i];
ansPoint2 = TempMerge[j];
}
}
}
return closest;
} double findTheClosest(int left, int right)
{
if(left>=right) return INF;
int mid = (left+right)>>;
double leftAns = findTheClosest(left, mid);
double rightAns = findTheClosest(mid+, right);
double ans = min(leftAns, rightAns);
int low = left, high = mid+;
while(distance(points[low], points[mid])>ans)low++;
while(high <= right && distance(points[high], points[mid])<=ans)high++;
ans = min(ans, calculateTheClosest(low, high-));
return ans;
} int main()
{
while(scanf("%d", &numOfPoint) == && numOfPoint)
{
initPoints();
sort(points, points+numOfPoint, cmp_X);
double ans = findTheClosest(, numOfPoint-);
printf("%.2lf\n", ans/);
//printf("The Point(%.2lf, %.2lf) and Point(%.2lf, %.2lf) is %lf\n", ansPoint1.x,ansPoint1.y,ansPoint2.x,ansPoint2.y,ans);
}
return ;
}

HDU1007最近点对(分治)的更多相关文章

  1. Quoit Design(最近点对+分治)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...

  2. HDU1007(最近点对)

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

  3. 平面最近点对(分治nlogn)

    平面最近点对,是指给出平面上的n个点,寻找点对间的最小距离 首先可以对按照x为第一关键字排序,然后每次按照x进行分治,左边求出一个最短距离d1,右边也求出一个最短距离d2,那么取d=min(d1, d ...

  4. HDU1007 Quoit Design 【分治】

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

  5. UVA 10245 The Closest Pair Problem 最近点问题 分治算法

    题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...

  6. Quoit Design(hdu1007)最近点对问题。模版哦!

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

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

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

  8. <算法竞赛入门经典> 第8章 贪心+递归+分治总结

    虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...

  9. ACM模板_axiomofchoice

    目录 语法 c++ java 动态规划 多重背包 最长不下降子序列 计算几何 向量(结构体) 平面集合基本操作 二维凸包 旋转卡壳 最大空矩形 | 扫描法 平面最近点对 | 分治 最小圆覆盖 | 随机 ...

随机推荐

  1. 淘宝主搜索离线集群完成Hadoop 2

    淘宝搜索离线dump集群(hadoop&hbase)2013进行了几次重大升级,本文中将这些升级的详细过程.升级中所遇到的问题以及这些问题的解决方案分享给大家.至此,淘宝主搜索离线集群完全进入 ...

  2. postgresql大批量数据导入方法

    一直没有好好关注这个功能,昨天看了一下,数据库插入有瓶颈,今天研究了一下: 主要有以下方案: 1.使用copy从文件导入: copy table_001(a, b, "f", d, ...

  3. android架构图示

    Android系统架构和一些普遍的操作系统差不多,都是采用了分层的架构,从他们之间的架构图看,Android系统架构分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核 ...

  4. impdp之remap_schema参数

    众所周知,IMP工具的FROMUSER和TOUSER参数可以实现将一个用户的的数据迁移到另外一个用户.同样的功能在IMPPDP工具中如何得以体现呢?答案就是:使用IMPPDP的REMAP_SCHEMA ...

  5. liunx的目录结构

    linux目录结构的最顶端是/目录 我们一般都称为root目录. linux有四种文件类型,分别是普通文件,目录文件,连接文件,特殊文件,可以用file来识别. 普通文件:文本文件 二进制文件 图像文 ...

  6. 【转】Android Interface的使用

    原文网址:http://www.3g-edu.org/Android_free/art115.htm Android应用程序采用Java语言编写,Java语法和C/C++有很大的相似性,但也有一些特别 ...

  7. JBPM4入门——9.自动节点单线执行

    JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流程图的插件 JBPM4入门——3.JBPM4开发环境的搭建 JBPM4入门—— ...

  8. C# 使用NPlot绘图

    首先要将下载的NPlot.dll加到工具箱里,拖一个控件到窗体上,声明using NPlot. 一.入门 1. 对所绘的图进行打印与保存 private void print() { myPlot.P ...

  9. python+selenium环境搭建

    这里主要基于windows平台. 下载python.http://python.org/getit/ 下载setuptools [python的基础包工具].http://pypi.python.or ...

  10. selenium python (十三)对于分页的处理

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #对于web上分页的功能,一般做如下操作:    #获取总页数    # ...