题意:数组二维空间内的点,求最近的俩个点的距离.

根据x排序,求左部分的最近距离,右部分最近距离,然后以中点,当前距离为半径,计算所有的点距离.

#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
#include "stdio.h"
namespace cc
{
using std::cout;
using std::endl;
using std::cin;
using std::map;
using std::vector;
using std::string;
using std::sort;
using std::priority_queue;
using std::greater;
using std::vector;
using std::swap;
using std::stack;
using std::bitset; class Node
{
public:
double x;
double y;
Node() {};
Node(double x, double y) :x(x), y(y) {}; }; constexpr int N = ;
Node a[N + ]; bool cmp(Node& a, Node& b)
{
return a.x < b.x;
} double dist(Node a, Node b)
{
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
} double minDist(int l, int r)
{
if (l > r) return ;
int mid = (l + r) / ;
int s = mid, e = mid;
double d = std::min(minDist(l, mid - ), minDist(mid + , r));
while (s >= l && d > a[mid].x - a[s].x) s--;
while (e <= r && d > a[e].x- a[mid].x) e++;
for (int i = s + ;i < e;i++)
{
for (int j = i + ;j < e;j++)
{
d = std::min(dist(a[i], a[j]), d);
}
}
return d;
} void solve()
{ double s, e;
int n;
while (cin >> n && n)
{
for (int i = ;i < n;i++)
{
cin >> s >> e;
Node node(s, e);
a[i] = node;
}
sort(a, a + n, cmp);
double d = minDist(,n-);
if (d >= )
{
cout << "INFINITY" << endl; }
else
{
printf("%.4lf\n",d);
}
}
} }; int main()
{ #ifndef ONLINE_JUDGE
freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
cc::solve(); return ;
}

另外,这个题裸奔也行的

#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
#include "stdio.h"
namespace cc
{
using std::cout;
using std::endl;
using std::cin;
using std::map;
using std::vector;
using std::string;
using std::sort;
using std::priority_queue;
using std::greater;
using std::vector;
using std::swap;
using std::stack;
using std::bitset; class Node
{
public:
double x;
double y;
Node() {};
Node(double x, double y) :x(x), y(y) {}; }; constexpr int N = ;
Node a[N + ]; bool cmp(Node& a, Node& b)
{
return a.x < b.x;
} double dist(Node a, Node b)
{
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
} double minDist(int l, int r)
{
if (l > r) return ;
int mid = (l + r) / ;
int s = mid, e = mid;
double d = std::min(minDist(l, mid - ), minDist(mid + , r));
while (s >= l && d > a[mid].x - a[s].x) s--;
while (e <= r && d > a[e].x- a[mid].x) e++;
for (int i = s + ;i < e;i++)
{
for (int j = i + ;j < e;j++)
{
d = std::min(dist(a[i], a[j]), d);
}
}
return d;
} void solve()
{ double s, e;
int n;
while (cin >> n && n)
{
for (int i = ;i < n;i++)
{
cin >> s >> e;
Node node(s, e);
a[i] = node;
}
sort(a, a + n, cmp);
double d = ;
for (int i = ;i < n;i++)
{
for (int j = i + ;j < n;j++)
{
d = std::min(dist(a[i],a[j]),d);
}
}
if (d >= )
{
cout << "INFINITY" << endl; }
else
{
printf("%.4lf\n",d);
}
}
} }; int main()
{ #ifndef ONLINE_JUDGE
freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
cc::solve(); return ;
}

uva-10245-分治的更多相关文章

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

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

  2. UVA 10245 The Closest Pair Problem【分治】

    题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269 题意: 求平面最近点对. 分析: 经典问题. n比 ...

  3. UVa 10245 The Closest Pair Problem (分治)

    题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...

  4. UVA 10245 - The Closest Pair Problem

    Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...

  5. UVa 1608 (分治 中途相遇) Non-boring sequences

    预处理一下每个元素左边和右边最近的相邻元素. 对于一个区间[l, r]和区间内某一个元素,这个元素在这个区间唯一当且仅当左右两边最近的相邻元素不在这个区间内.这样就可以O(1)完成查询. 首先查找整个 ...

  6. uva 10245 The Closest Pair Problem_枚举

    题意:求任意两点之间的距离的最少一个距离 思路:枚举一下就可以了 #include <iostream> #include<cstdio> #include<cmath& ...

  7. uva 10245 近期点对问题

    分治法的典例 当练手了 奇妙的是.使用inplace_merge按说应该是O(n)的算法.可是用sort nlogn的算法反而更快 先上快排版 #include <cstdio> #inc ...

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

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

  9. CDQ分治入门 + 例题 Arnooks's Defensive Line [Uva live 5871]

    CDQ分治入门 简介 CDQ分治是一种特别的分治方法,它由CDQ(陈丹琦)神犇于09国家集训队作业中首次提出,因此得名.CDQ分治属于分治的一种.它一般只能处理非强制在线的问题,除此之外这个算法作为某 ...

  10. UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

随机推荐

  1. 打开本页N秒后跳转页面

    在head标签里面 <meta http-equiv="refresh" content="4;url=" />

  2. leetcode python 011

    ####给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点.##绘制n条垂直线,使得线i的两个端点位于(i,ai)和(i,0).##找到两条线,它们与x轴一起形成一个容器,这 ...

  3. 8.6 C++文本文件的读写操作

    参考:http://www.weixueyuan.net/view/6412.html 总结: 文件类型: 计算机上的文件其实是数据的集合,对文件的读写归根结底还是对数据的读写操作.文件可以大致分为两 ...

  4. Jmeter处理返回结果的值

    接口测试中,获取返回结果的值可以用插件JSON Path Extractor 此插件可以直接处理json,通过key来取值. 该插件下载地址为:http://jmeter-plugins.org/wi ...

  5. linux安装mysql图文教程

    ---恢复内容开始--- 1.下载mysql [root@localhost ~]# yum install mysql mysql-server 输入y 输入y 输入y 下载完成 接下来我们要使用w ...

  6. java学习笔记37(sql工具类:JDBCUtils)

    在之前的内容中,我们发现,当我们执行一条语句时,每新建一个方法,就要重新连接一次数据库,代码重复率很高,那么能不能把这些重复代码封装成一个类呢,我们学习方法时,就学习到方法就是为了提高代码的利用率,所 ...

  7. «面向对象程序设计(java)»第三周学习总结 周强 201771010141

    实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟悉PTA平台线上测试环境: (3)掌握Java语言构造基本程序语法知识(ch1-ch3): (4)利用已 ...

  8. javax/servlet/jsp/jstl/core/Config

    javax/servlet/jsp/jstl/core/Config springmvc出现的问题. 尝试了各种jar,问题依旧. DispatcherServlet配置如下. <bean id ...

  9. 学习笔记TF047:PlayGround、TensorBoard

    PlayGround.http://playground.tensorflow.org .教学目的简单神经网络在线演示.实验图形化平台.可视化神经网络训练过程.在浏览器训练神经网络.界面,数据(DAT ...

  10. Oracle的导入和导出

    导出命令: EXP 用户名/密码@数据库名  BUFFER=64000 file=G:\dmp\full1.dmp  OWNER=用户名 导入命令: IMP 用户名/密码@数据库名 BUFFER=64 ...