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 ...
随机推荐
- Linux之expr命令详解
expr命令: expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串. –格式为: expr Expression(命令读入Expressi ...
- springboot学习之构建简单项目搭建
概述 相信对于Java开发者而言,spring和springMvc两个框架一定不陌生,这两个框架需要我们手动配置的地方非常多,各种的xml文件,properties文件,构建一个项目还是挺复杂的,在这 ...
- GraphHttpClient概述
博客地址:http://blog.csdn.net/FoxDave 目前这个东西还在预览阶段,所以不推荐在正式生产环境中使用. 我们可以使用Microsoft Graph接口来构建强大的解决方案来访问 ...
- Unity发布WebGl注意事项
unity 版本是5.5,不过看了2017的文档好像也是差不多,绝大部分都是根据官方文档,希望有帮助,如果有错误或者你知道更多这方面的只是,请告知下,大恩言谢. 1:对webgl发布的工程文件说明 ...
- nodejs - 1)上传图片 ,并显示 , 2)模块 formidable
1.代码: 1-1: 入口文件: index.js var server = require('./server'); var router = require("./router" ...
- .NET并行计算和并发8-QueueUserWorkItem异步
QueueUserWorkItem方法将非常简单的任务排入队列 下面这个简单的代码,涉及到资源竞争问题,如果主线程先争取到资源,如果没有等待 一段时间,那么QueueUserWorkItem申请的 ...
- Font 'C:\WINDOWS\FONTS\msyh.ttc' with 'Identity-H' is not recognized
在生成PDF使用windows自带字体雅黑的时候不停的报“Font 'C:\WINDOWS\FONTS\msyh.ttc' with 'Identity-H' is not recognized”的错 ...
- 什么是Maven项目
1.通俗理解Maven:https://blog.csdn.net/shuzhe66/article/details/45009175 个人总结: Maven项目会有pom文件! 当前的项目需要依赖其 ...
- 13--Python入门--文件读写--CSV&Excel文件
EXCEL文件 import pandas as pd excel=pd.read_excel('read_excel.xlsx') print(excel) CSV文件 import pandas ...
- L2-007. 家庭房产(并查集)*
L2-007. 家庭房产 参考博客 #include <iostream> #include <cstdio> #include <cstring> #includ ...