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

根据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. Visual Basic 2017 操作Excel和word【1】持续更新……

    我坚持在VB的路上走到黑…………  清单1.1  从应用程序对象导航到Excel中的工作表  Dim myWorkbooks As Excel.Workbooks = app.Workbooks ) ...

  2. Java容器解析系列(3) List AbstractList ListIterator RandomAccess fail-fast机制 详解

    做为数据结构学习的常规,肯定是先学习线性表,也就是Java中的List,开始 Java中List相关的类关系图如下: 此篇作为对Java中相关类的开篇.从上图中可以看出,List和AbstractLi ...

  3. NSIS脚本 打包安装程序

    相关工具 nsis http://nsis.sourceforge.net/Special_Builds HM NIS Edit http://hmne.sourceforge.net/ 例子: ; ...

  4. Android:进程优先级

    进程优先级 优先级 服务 说明 高优先级 前台进程 ①该进程包含正在与用户进行交互的界面组件,比如一个Activity. ②进程服务被Activity调用,而且这个Activity正在与用户进行交互 ...

  5. quora 的东西就是不一样

    Coding is just a part of process of problem solving, You should need to understand the underlying pr ...

  6. 北大poj- 1006

    生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 133189   Accepted: 42577 Descripti ...

  7. FCC JS基础算法题(0):Reverse a String(翻转字符串)

    题目描述: 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串.你的结果必须得是一个字符串. 算法: function reverseString(str) { ...

  8. 小程序仿QQ侧滑例子

    缩放:wxml <!--page/one/index.wxml--> <view class="page"> <view class="pa ...

  9. 渲染标签 - v-text

    <!DOCTYPE html><html><head>    <meta charset="utf-8">    <title ...

  10. COMBIN14简单应用

    目录 案例1 说明 APDL代码 结果 案例2 说明 APDL代码 结果 案例3 说明 APDL代码 结果 参考网址:http://blog.sina.com.cn/s/blog_65936c2301 ...