【hdu1007】最近点对
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】最近点对的更多相关文章
- Quoit Design(hdu1007)最近点对问题。模版哦!
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU1007(最近点对)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU1007最近点对(分治)
http://acm.hdu.edu.cn/showproblem.php?pid=1007 直接见代码吧.不过这个是N*logN*logN的 尽管如此,我怎么感觉我的比他们的还快??? #inclu ...
- (hdu1007)Quoit Design,求最近点对
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- hdu1007 平面最近点对(暴力+双线程优化)
突发奇想,用双线程似乎可以优化一些暴力 比如说平面最近点对这个题目,把点复制成2份 一份按照x排序,一份按照y排序 然后双线程暴力处理,一份处理x,一份处理y 如果数据利用x递减来卡,那么由于双线程, ...
- HDU-1007 Quoit Design 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少.. //STATU ...
- 最近点对HDU1007
利用二分的方法来计算,应该是说利用分治的方法吧! 刚开始感觉时间会爆 后来发现嘎嘎居然没有 ,嗨自己算错了时间: #include <iostream> #include<cstdi ...
- ICP算法(Iterative Closest Point迭代最近点算法)
标签: 图像匹配ICP算法机器视觉 2015-12-01 21:09 2217人阅读 评论(0) 收藏 举报 分类: Computer Vision(27) 版权声明:本文为博主原创文章,未经博主允许 ...
- Quoit Design---hdu1007(最近点对问题 分治法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 题意:给你n(2<=n<=10^6)个点的坐标,然后找到两个点使得他们之间的距离最小 ...
随机推荐
- 学习JVM参数前必须了解的
JVM参数是什么 大家照相通常使用手机就够用了,但是针对发烧友来说会使用更专业的设备,比如单反相机,在单反里有好几个模式,P/A/S/M,其中P是傻瓜模式,程序会自动根据环境设置快门速度和光圈大小,以 ...
- linux 文件的查找和压缩
1.使用 locate 命令 需要安装:yum install mlocate -y 创建或更新 slocate/locate 命令所必需的数据库文件:updatedb 作用:搜索不经常改变的文件如配 ...
- Java面试系列第一篇-基本类型与引用类型
这篇文章总结一下我认为面试中最应该掌握的关于基本类型和引用类型的面试题目. 面试题目1:值传递与引用传递 对于没有接触过C++这类有引用传递的Java程序员来说,很容易误将引用类型的参数传递理解为引用 ...
- 初学者的Pygame安装教程
最近在自学python,在看完了些基础知识之后,准备写个小项目[外星人入侵],这个项目需要安装pygame. 所以就在网上找到了两个下载地址https://bitbucket.org/pygame/p ...
- thinkphp--多表查询
我们可以将两个表连起来一起查询数据,我现在有两张表,一个是feedback表和member表,如图: 总目录: 上代码: $where = array(); $"; $Model = M(' ...
- mac OS vi/vim 使用教程
vi/vim 的使用 基本上 vi/vim 共分为三种模式 分别是 命令模式(Command mode) 输入模式(Insert mode) 底线命令模式(Last line mode) 命令模式: ...
- XmlSerializer .NET 序列化、反序列化
序列化对象 要序列化对象,首先创建要序列化的对象并设置其公共属性和字段.为此,您必须确定要将XML流存储的传输格式,作为流或文件. 例如,如果XML流必须以永久形式保存,则创建一个FileStre ...
- XML--XML作用
XML 把数据从 HTML 分离 如果你需要在 HTML 文档中显示动态数据,那么每当数据改变时将花费大量的时间来编辑 HTML. 通过 XML,数据能够存储在独立的 XML 文件中.这样你就可以专注 ...
- POJ - 2251 Dungeon Master (搜索)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- js世家委托详解
事件原理 通过div0.addElementListener来调用:用法:div0.addElementListener(事件类型,事件回调函数,是否捕获时执行){}.1.事件类型(type):必须是 ...