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

分治法的经典应用,复杂度可以证明为nlognlogn

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <string>
#include <ctime>
#include <queue>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define eps 0.0000001
#define lowbit(x) ((x) & -(x))
#define memc(a, b) memcpy(a, b, sizeof(b))
#define x_x(a) ((a) * (a))
using namespace std;
#define LL __int64
#define DB double
#define pi 3.14159265359
#define MD 10000007
#define INF (int)1e9
struct Point {
double x, y;
Point(double x = , double y = ):x(x), y(y) {}
void inp() {
scanf("%lf%lf", &x, &y);
}
bool operator < (const Point &a) const {
return x < a.x || x == a.x && y < a.y;
}
} pn[], tmp[];
int cmpy(Point i, Point j)
{
return i.y < j.y;
}
double dist(Point a, Point b)
{
return sqrt(x_x(a.x - b.x) + x_x(a.y - b.y));
}
double solve(int l, int r)
{
if(l == r) return INF;
int m = (l + r) >> ;
double d1 = solve(l, m), d2 = solve(m + , r), d = min(d1, d2);
int nn = ;
for(int i = l; i <= r; i++) {
if(dist(pn[i], pn[m]) <= d) {
tmp[++nn] = pn[i];
}
}
sort(tmp + , tmp + nn, cmpy);
for(int i = ; i < nn; i++) {
double mind = INF;
for(int j = i + ; j <= nn && tmp[j].y - tmp[i].y <= d; j++) {
mind = min(mind, dist(tmp[i], tmp[j]));
}
d = min(d, mind);
}
return d;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
while(cin>> n, n) {
for(int i = ; i <= n; i++) pn[i].inp();
sort(pn + , pn + + n);
double t = solve(, n);
printf("%.2f\n", t / );
}
return ;
}

【hdu1007】最近点对的更多相关文章

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

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

  2. HDU1007(最近点对)

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

  3. HDU1007最近点对(分治)

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 直接见代码吧.不过这个是N*logN*logN的 尽管如此,我怎么感觉我的比他们的还快??? #inclu ...

  4. (hdu1007)Quoit Design,求最近点对

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  5. hdu1007 平面最近点对(暴力+双线程优化)

    突发奇想,用双线程似乎可以优化一些暴力 比如说平面最近点对这个题目,把点复制成2份 一份按照x排序,一份按照y排序 然后双线程暴力处理,一份处理x,一份处理y 如果数据利用x递减来卡,那么由于双线程, ...

  6. HDU-1007 Quoit Design 平面最近点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少.. //STATU ...

  7. 最近点对HDU1007

    利用二分的方法来计算,应该是说利用分治的方法吧! 刚开始感觉时间会爆 后来发现嘎嘎居然没有 ,嗨自己算错了时间: #include <iostream> #include<cstdi ...

  8. ICP算法(Iterative Closest Point迭代最近点算法)

    标签: 图像匹配ICP算法机器视觉 2015-12-01 21:09 2217人阅读 评论(0) 收藏 举报 分类: Computer Vision(27) 版权声明:本文为博主原创文章,未经博主允许 ...

  9. Quoit Design---hdu1007(最近点对问题 分治法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:给你n(2<=n<=10^6)个点的坐标,然后找到两个点使得他们之间的距离最小 ...

随机推荐

  1. Python冒泡排序算法及其优化

    冒泡排序 所谓冒泡,就是将元素两两之间进行比较,谁大就往后移动,直到将最大的元素排到最后面,接着再循环一趟,从头开始进行两两比较,而上一趟已经排好的那个元素就不用进行比较了.(图中排好序的元素标记为黄 ...

  2. Numpy学习-(1)

    记录我学习Numpy过程 1. 介绍 (1)NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库 ...

  3. Spring Data REST不完全指南(三)

    上一篇我们介绍了使用Spring Data REST时的一些高级特性,以及使用代码演示了如何使用这些高级的特性.本文将继续讲解前面我们列出来的七个高级特性中的后四个.至此,这些特性能满足我们大部分的接 ...

  4. google无法播放mp4 chrome无法播放h264

    写在前面 我在chrome上无法播放h264+Acc的mp4,在firefox.ie都可以播放,而且此mp4在vlc终可以正常播放. 视频链接:http://106.14.221.185:7001/p ...

  5. SringMVC入门程序

    Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架 1.Spring优点 轻量级,简单易学 高效 , 基于请求响应的MVC框架 与Spring兼 ...

  6. 小白初学Java的一点点收获

    作为刚刚学习Java没有几天的小白,我想把我在学习过程中,所学习到的知识和注意事项和大家一起分享分享.在这个过程中,希望大家可以有所收获,有什么不对的地方,希望大家指出并且私信我. 首先说说第一次记事 ...

  7. python 工具链 虚拟环境和包管理工具 pipenv

    Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, ...

  8. GraphicsLab Project 之 Curl Noise

    作者:i_dovelemon 日期:2020-04-25 主题:Perlin Noise, Curl Noise, Finite Difference Method 引言 最近在研究流体效果相关的模拟 ...

  9. foreach里的按引用传值问题

    1.foreach($arr as $k=>&$v){ } 这样循环时候最后一个结果前边会有&,出现输出不了的情况,这时候只需要加一个unset($v),加在循环里和外均可. 2 ...

  10. Python 替换文本中的某些词语

    https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python from tempfil ...