uva-10245-分治
题意:数组二维空间内的点,求最近的俩个点的距离.
根据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-分治的更多相关文章
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- UVA 10245 The Closest Pair Problem【分治】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269 题意: 求平面最近点对. 分析: 经典问题. n比 ...
- UVa 10245 The Closest Pair Problem (分治)
题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...
- UVA 10245 - The Closest Pair Problem
Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...
- UVa 1608 (分治 中途相遇) Non-boring sequences
预处理一下每个元素左边和右边最近的相邻元素. 对于一个区间[l, r]和区间内某一个元素,这个元素在这个区间唯一当且仅当左右两边最近的相邻元素不在这个区间内.这样就可以O(1)完成查询. 首先查找整个 ...
- uva 10245 The Closest Pair Problem_枚举
题意:求任意两点之间的距离的最少一个距离 思路:枚举一下就可以了 #include <iostream> #include<cstdio> #include<cmath& ...
- uva 10245 近期点对问题
分治法的典例 当练手了 奇妙的是.使用inplace_merge按说应该是O(n)的算法.可是用sort nlogn的算法反而更快 先上快排版 #include <cstdio> #inc ...
- <算法竞赛入门经典> 第8章 贪心+递归+分治总结
虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...
- CDQ分治入门 + 例题 Arnooks's Defensive Line [Uva live 5871]
CDQ分治入门 简介 CDQ分治是一种特别的分治方法,它由CDQ(陈丹琦)神犇于09国家集训队作业中首次提出,因此得名.CDQ分治属于分治的一种.它一般只能处理非强制在线的问题,除此之外这个算法作为某 ...
- UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
随机推荐
- python自学第12天 模块
json , pickle模块 json,用于字符串和python数据类型间进行转换 pickle,用于python特有的类型和python数据类型间的进行转换 shelve 模块 是一个简单的k,v ...
- 蓝牙协议分析(5)_BLE广播通信相关的技术分析
1. 前言 大家都知道,相比传统蓝牙,蓝牙低功耗(BLE)最大的突破就是加大了对广播通信(Advertising)的支持和利用.关于广播通信,通过“玩转BLE(1)_Eddystone beacon” ...
- Oracle数据库死锁和MySQL死锁构造和比较
最近在复习数据库的事务隔离性,顺便构造了一下在Oracle上和MySQL上的死锁以比较异同. 在Oracle上面的实验 在Oracle中,因为是显式提交,所以默认可以认为在一个会话中若没有使用comm ...
- VM下如何装虚拟机?
1.打开VM,点击创建新的虚拟机 2.选择典型,点击下一步 3.选择下载好的iso文件,如下: 4.输入如下参数,点击下一步:
- Java第三次作业——面向对象基础(封装)
Java第三次作业--面向对象基础(封装) (一)学习总结 1.什么是面向对象的封装性,Java中是如何实现封装性的?试举例说明. 封装性 封装性是面向对象的方法所应遵循的一个重要原则,它有两个含义: ...
- Visual Studio AI环境记录(Windows10)
一.环境 Windows [版本 10.0.15063]64位 Git-2.14.1 64位[官网下载] TortoiseGit-2.5.0.0 64位[官网下载],这是一个Git 客户端,外号&qu ...
- 生产redis client 链接报:ERR max number of clients reached 含义: 达到最大客户端数错误
1.通过netstat 命令查看TCP又11822个连接 (netstat命令是一个监控TCP/IP网络的非常有用的工具) 2.默认redis最大的连接数10000 ,但是此时无法连接redis客户 ...
- Web前端入门教程之浏览器兼容问题及解决方法
JavaScript 被称为JS,是作为浏览器的内置脚本语言,为我们提供操控浏览器的能力,可以让网页呈现出各种特殊效果,为用户提供友好的互动体验.JS是Web前端入门教程中的重点和难点,而浏览器兼容性 ...
- php 会话控制(关于session的维护与生命周期)
Session是由应用服务器维持的一个服务器端的存储空间,用户在连接服务器时,会由服务器创建生成一个唯一的sessionID,用该sessionID为标识符来存取服务器端的Session存储空间,在会 ...
- Linux双线双网卡双IP双网关设置方法
机房上架了一台测试机,系统是Ubuntu 9.04 X64的系统,母机IBM X336机器.用户需求是双线,故采用一个网卡配置电信地址,另一个网卡配置联通地址,安装好系统后配置好IP发现联通地址和电信 ...