poj 3714 Raid(平面最近点对)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 7473 |
Accepted: 2221 |
Description
After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.
The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?
Input
The first line is a integer T representing the number of test cases. Each test case begins with an integer N (1 ≤ N ≤ 100000). The next N lines describe the positions of the stations. Each line consists of two i
ntegers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station. The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.
Output
For each test case output the minimum distance with precision of three decimal placed in a separate line.
Sample Input
2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
Sample Output
1.414
0.000
Source
1对原数组依据x左标从小到大排序。
2二分数组,左边求出最小值,右边求出最小值,我们求最小的。
3找出对于左右两边的可能小于当前最小值的最近点对,更新最小值。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring> #define DX(x) ((x) * (x))
using namespace std; const int MAX = + ;
const double INF = 10e100;
struct Point
{
double x, y;
int index,flag;//flag分类!!
}A[MAX], B[MAX], C[MAX]; bool compX(const Point& a, const Point& b)
{
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
} bool compY(const Point& a, const Point& b)
{
if(a.y == b.y)
return a.x < b.x;
return a.y < b.y;
} inline double getMin(double a, double b)
{
return a < b ? a : b;
} double getDist(Point& a, Point& b)
{
if(a.flag==b.flag)return INF;//同类规定距离为oo!!
return sqrt(DX(a.x - b.x) + DX(a.y - b.y));
} void merge(Point p[], Point q[], int s, int m, int t)
{
int i, j, k;
for(i = s, j = m + , k = s; i <= m && j <=t;)
{
if(q[i].y > q[j].y)
p[k++] = q[j], j++;
else
p[k++] = q[i], i++;
}
while(i <= m)
p[k++] = q[i++];
while(j <= t)
p[k++] = q[j++];
} double closest(Point a[], Point b[], Point c[], int p, int q)
{
if(q - p == )
return getDist(a[p], a[q]);
if(q - p == )
{
double x1 = getDist(a[p], a[q]);
double x2 = getDist(a[p + ], a[q]);
double x3 = getDist(a[p], a[p + ]);
return getMin(x1, getMin(x2, x3));
}
int i, j, k, m = (p + q) / ;
double d1, d2;
for(i = p, j = p, k = m + ; i <= q; ++i)
{
if(b[i].index <= m)
c[j++] = b[i];
else
c[k++] = b[i];
}
d1 = closest(a, c, b, p, m);
d2 = closest(a, c, b, m + , q);
double dm = getMin(d1, d2);
merge(b, c, p, m, q);
for(i = p, k = p; i <= q; ++i)
{
if(fabs(b[i].x - b[m].x) < dm)
c[k++] = b[i];
}
for(i = p; i < k; ++i)
for(j = i + ; j < k && c[j].y - c[i].y < dm; ++j)
{
double temp = getDist(c[i], c[j]);
if(temp < dm)
dm = temp;
}
return dm;
}
int main()
{
//freopen("in.txt", "r", stdin);
int T,n;
scanf("%d",&T);
while( T--)
{
scanf("%d", &n);
for(int i = ; i < n; ++i)
scanf("%lf%lf", &A[i].x, &A[i].y),A[i].flag=;
for(int i = ; i < n; ++i)
scanf("%lf%lf", &A[i+n].x, &A[i+n].y),A[i+n].flag=;
sort(A, A + *n, compX);
for(int i = ; i < *n; ++i)
A[i].index = i;
memcpy(B, A, *n * sizeof(B[]));
sort(B, B + *n, compY);
double d = closest(A, B, C, , *n - );
printf("%.3f\n", d);
}
return ;
}
poj 3714 Raid(平面最近点对)的更多相关文章
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- POJ 3714 Raid
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- 『Raid 平面最近点对』
平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...
- POJ 3714 Raid(计算几何の最近点对)
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- POJ 3714 Raid(平面近期点对)
解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...
- poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】
题目: http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...
- POJ-3714 Raid 平面最近点对
题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...
- POJ 3714 Raid 近期对点题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- (洛谷 P1429 平面最近点对(加强版) || 洛谷 P1257 || Quoit Design HDU - 1007 ) && Raid POJ - 3714
这个讲的好: https://phoenixzhao.github.io/%E6%B1%82%E6%9C%80%E8%BF%91%E5%AF%B9%E7%9A%84%E4%B8%89%E7%A7%8D ...
随机推荐
- 冲刺周日 Fighting SunDay
一.SunDay照片 二.项目分工 三.今日份燃尽图 四.项目进展 码云团队协同环境构建完毕 利用Leangoo制作任务分工及生成燃尽图 完成AES加解密部分代码 用代码实现对文件的新建.移动.复制. ...
- Vue/Element-ui 安装搭建开发环境(一)
Element 是饿了么全段开发团队推出的一套基于 vue.js2.0 的 PC Web 端开发框架. Element 中文文档:https://element.eleme.cn/#/zh-CN 1. ...
- 分布式工作流任务调度系统Easy Scheduler正式开源
分布式工作流任务调度系统Easy Scheduler正式开源 1.背景 在多位技术小伙伴的努力下,经过近2年的研发迭代.内部业务剥离及重构,也经历一批种子用户试用一段时间后,EasyScheduler ...
- NLP大赛冠军总结:300万知乎多标签文本分类任务(附深度学习源码)
NLP大赛冠军总结:300万知乎多标签文本分类任务(附深度学习源码) 七月,酷暑难耐,认识的几位同学参加知乎看山杯,均取得不错的排名.当时天池AI医疗大赛初赛结束,官方正在为复赛进行平台调 ...
- final finalize finally throw throws try catch
什么是finalize()方法 finalize()方法什么时候被调用 参见网址 析构函数(finalization)的目的是什么 final 和 finalize 的区别 final以下参见网址 f ...
- 事务的ACID属性
事务,一个操作序列,这些操作要么都执行,要么都不执行,是一个不可分割的整体. ACID为事务的四大属性 原子性(Atomic):指整个数据库事务是不可分割的工作单位.只有使据库中所有的操作执行成功,才 ...
- 修改web项目发布路径
Eclipse中用Tomcat发布的Web项目,更改其部署路径 我的Eclipse的工作目录是D:/workspace先配置Tomcat 选择你的tomcat版本 点击next 这里先不要把项目添加进 ...
- PADS LAYOUT的一般流程
1.概述 本文档的目的在于说明使用PADS的印制板设计软件PowerPCB进行印制板设计的流程和一些注意事项,为一个工作组的设计人员提供设计规 范,方便设计人员之间进行交流和相互检查. 2.设计 ...
- Factory Kit【其他模式】
Factory Kit public class FactoryKit { /** * Factory Kit:它定义了一个包含不可变内容的工厂,并使用独立的构建器和工厂接口来处理对象的创建. */ ...
- Linux_SELinux使用
目录 目录 SELinux SElinux的应用 修改 SELinux 下次启动模式 修改 SELinux 上下文 上下文的快速模仿 SELinux布尔值 图形化管理SElinux SELinux错误 ...