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

根据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. day_7

    r 读w 写a 追加写 f = open('s.txt',mode='r',encoding='utf-8') ff = f.readline() #读取时一行一行的时候末尾会跟着\nprint(ff ...

  2. pass,break,continue的使用场景

    # ### 关键字的使用 pass / break / continue # pass 过 起到占位的作用 if 5 == 5: pass print(123) # break 只能应用在循环当中 用 ...

  3. C#添加文字水印

    使用的是iTextSharp添加PDF水印,由于是接口动态生成PDF,所以采用的是全部是内存流的形式,而且水印是平铺是.iTextSharp版本是5.5 /// <summary> /// ...

  4. Centos6.8 搭建Nginx服务器

    Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,其特点是占有内存少,并发能力强,业界内的评价一直很不错,反正用过的都说好,虽然我还 ...

  5. HttpWebRequest post 请求超时问题

    在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步, 发送一个请求, 包含一个Expect:100-continue, ...

  6. 第三章 C#程序结构[3.2 选择结构的应用(Windows窗体应用程序)(四)]

    [案例]设计一个顾客选购商品的系统.其中,顾客身份有两类,一类是VIP,另一类是普通会员:商品种类有3种.分别是上衣.裤子和鞋子.其中,VIP享受8折优惠和商店赠送的礼品,而普通会员都不享受.单击[确 ...

  7. 第一章 C#入门 (Windows窗体应用程序)(一)

    我的第一个窗体应用程序(一) [案例说明]  在文本框中显示一行文字“Hello C#!”,单击[显示]按钮后在文本框中显示文字:单击[清除]按钮后清除文本框中的内容. [案例实现步骤] 1.新建项目 ...

  8. C# System.IO和对文件的读写操作

      System.IO命名空间中常用的非抽象类 BinaryReader 从二进制流中读取原始数据 BinaryWriter 从二进制格式中写入原始数据 BufferedStream 字节流的临时存储 ...

  9. HTML5操作麦克风获取音频数据(WAV)的一些基础技能

    基于HTML5的新特性,操作其实思路很简单. 首先通过navigator获取设备,然后通过设备监听语音数据,进行原始数据采集. 相关的案例比较多,最典型的就是链接:https://developer. ...

  10. windows 安装lua-5.3.4 --引用自https://blog.csdn.net/wangtong01/article/details/78296369

    版权声明:本文为博主原创文章,转载时请标明出处.http://blog.csdn.net/wangtong01 https://blog.csdn.net/wangtong01/article/det ...