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找出对于左右两边的可能小于当前最小值的最近点对,更新最小值。
 
本题很水,直接套模板就秒过!!!
这题目需要区分一下点,让我们求的是闪兵到任意一个核电站的最短距离,加一个标志就可以了。(若是同一类的点就规定距离为oo!!)
 
 #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(平面最近点对)的更多相关文章

  1. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

  2. POJ 3714 Raid

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

  3. 『Raid 平面最近点对』

    平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...

  4. POJ 3714 Raid(计算几何の最近点对)

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

  5. POJ 3714 Raid(平面近期点对)

    解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...

  6. poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】

    题目:  http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...

  7. POJ-3714 Raid 平面最近点对

    题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...

  8. POJ 3714 Raid 近期对点题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  9. (洛谷 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 ...

随机推荐

  1. 《Effective Java》读书笔记 - 3.对于所有对象都通用的方法

    Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几 ...

  2. IE与safari浏览器中时间格式问题

    一.问题内容 在js中处理Date时,发现IE与Safari和其他浏览器的支持方式不一致 1.例如:2017-01-01 12:00:00 在其他浏览器中,使用这个格式的字符串进行new Date操作 ...

  3. 十七、RF中的等待时间

    1.sleep:强制等待n秒 sleep  秒数 2.implicit wait 隐式等待 2.1 get selenium implicit wait  :取隐式等待时间,隐式等待时间默认为0 2. ...

  4. 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_4 Mybatis中使用Dao实现类的执行过程分析-查询方法

    delete方法没有并SqlSession的delete方法,而是调用的Upadte方法. 在测试类这里加断点. 实际的方法体内也加断点 运行测试方法,选择debug的方式 走到断点这里.会看到fac ...

  5. 【释疑】tp99、单实例qps

    tp99 tp99的定义 tp99 (top percentile 99),指一组数据从小到大排列,处于99%位置的数据的值.例如等差数列range(1,101),tp99=99 tp99优于平均值的 ...

  6. Scratch少儿编程系列:(一)版本的选择及安装

    工欲善其事必先利其器,为了使用Scratch,首先要到官网上下载相关软件. 官网链接地址为:https://scratch.mit.edu/download,我用的是Windows系统,下载对应的安装 ...

  7. 学习kettle遇到的问题

    一. 解决mysql连接缺少驱动问题:http://www.mamicode.com/info-detail-1724584.html 1.下载驱动 https://dev.mysql.com/dow ...

  8. Java——LinkedHashMap源码解析

    以下针对JDK 1.8版本中的LinkedHashMap进行分析. 对于HashMap的源码解析,可阅读Java--HashMap源码解析 概述   哈希表和链表基于Map接口的实现,其具有可预测的迭 ...

  9. Python_ONLINE_习题集_02 函数封装

    2.1 封装函数实现如下要求 例如:输入2,5 则求:2 + 22+222 + 2222+22222的和 参考答案: https://www.bilibili.com/read/cv4185619 d ...

  10. python winsound模块

    (目标:出现交易下单.结束成交.数据中断等信号的时候,PC 发出声音提醒.) python winsound模块 winsound是Python的内置包,无需下载.可以直接通过 import wins ...