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 integers 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

解析
第一次做平面最近点对的题,就遇到了一道变种......
最接近点对问题的提法是:给定平面上n个点,找其中的一对点,使得在n个点的所有点对中,该点对的距离最小。
但是在Raid这道题中,要求两个集合S1和S2中的最近点对,按理说代码应该更加复杂。
然而,这里找到了一个更简单的做法。
代码如下:
#include<cstring>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int T,n;
struct node{
double x,y;
}e[],d[];
bool cmp(node a,node b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
double dis(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
scanf("%d",&T);
while(T--)
{
double ans=1e11;
scanf("%d",&n);
for(register int i=;i<=n;++i)
{
scanf("%lf%lf",&e[i].x,&e[i].y);
}
for(register int i=;i<=n;++i)
{
scanf("%lf%lf",&d[i].x,&d[i].y);
}
sort(e+,e+n+,cmp);
sort(d+,d+n+,cmp);
int t = ;
for(int i=;i<=n;++i)
{
while(t<n&&cmp(e[t+],d[i])) ++t;//以d[i]的横坐标为分界线,将e点分为两部分
for(int j=t;j<=n;++j)//扫描{e}的第一部分
{
if(fabs(e[j].x-d[i].x)>ans) break;
ans= min(ans, dis(e[j],d[i]));
//往下扫描的同时,将ans向小更新。很显然,{e}经过排序之后,不需要几次就会break;
}
for(int j=t-;j;--j)//扫描{e}的另一部分
{
if(fabs(e[j].x-d[j].x)>ans) break;
ans = min(ans,dis(e[j],d[i]));//同上
}
}
printf("%.3f\n",ans);
}
return ;
}


 

【POJ3714】Raid:平面最近点对的更多相关文章

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

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

  2. 『Raid 平面最近点对』

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

  3. poj3714 Raid(分治求平面最近点对)

    题目链接:https://vjudge.net/problem/POJ-3714 题意:给定两个点集,求最短距离. 思路:在平面最近点对基础上加了个条件,我么不访用f做标记,集合1的f为1,集合2的f ...

  4. $Poj3714/AcWing\ Raid$ 分治/平面最近点对

    $AcWing$ $Sol$ 平面最近点对板子题,注意要求的是两种不同的点之间的距离. $Code$ #include<bits/stdc++.h> #define il inline # ...

  5. POJ 3741 Raid (平面最近点对)

    $ POJ~3741~Raid $ (平面最近点对) $ solution: $ 有两种点,现在求最近的平面点对.这是一道分治板子,但是当时还是想了很久,明明知道有最近平面点对,但还是觉得有点不对劲. ...

  6. POJ3714 Raid

    Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10625   Accepted: 3192 Description ...

  7. 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点

    平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...

  8. HDU-4631 Sad Love Story 平面最近点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近 ...

  9. HDU1007--Quoit Design(平面最近点对)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

随机推荐

  1. 【Spring Cloud学习之六】断路器-Hystrix

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 一.服务雪崩1.什么是服务雪崩分布式系统中经常会出现某个基础服务不可用造成整个系统不 ...

  2. 说说Java Web中的Web应用程序|乐字节

    大家好,我是乐字节的小乐,今天接着上期文章<Javaweb的概念与C/S.B/S体系结构>继续往下介绍Java Web ,这次要说的是web应用程序. 1. Web 应用程序的工作原理 W ...

  3. Maven依赖中scope的含义

    https://www.jianshu.com/p/7145f01ac3ad Maven依赖中scope的含义 整理一下Maven中Scope的详细作用,都是抄的别人内容整理了一下.参考: https ...

  4. 记事本 该文件含有unicode格式的字符 点确定就变乱码了,notePad++,UltraEditor等编辑器打开也变乱码?

    --问题 之前一直都是好的 今天电脑打开,发现电脑所有的TXT记事本,点开来全是乱码,甚至下过来的TXT,或者自己新建的TXT,打上中文字,点保存会弹出 该文件含有unicode格式的字符什么什么的, ...

  5. jQuery中$(this)与this的区别

    经常在写jQuery的时候分不清this 和 $(this),为了方便起见尽量不用this,只用$(this).但是今天在别人的代码的基础上改一些东西,又遇到了这个this,不得不把它弄明白. $(t ...

  6. MySQL表关系--外键

    一.外键前戏 如果我们把所有的信息都记录在一张表中会带来的问题: 1.表的结构不清晰 2.浪费磁盘空间 3.表的扩展性极差 所以我们要把这种表拆成几张不同的表,分析表与表之间的关系. 确定表与表之间的 ...

  7. Equalizing Two Strings CodeForces - 1256F (思维)

    大意: 给定两个串$s,t$, 每次操作任选长度$len$, 分别翻转$s,t$中一个长$len$的子串, 可以进行任意次操作, 求判断能否使$s$和$t$相同. 字符出现次数不一样显然无解, 否则若 ...

  8. 通过对比ASCII编码来理解Unicode编码

    Unicode是个规范,可以理解为一个索引表,世界上所有字符基本上在这个索引表中都能找到唯一一个数码与之对应,就像ASCII码表一样,也是一个规范,也可以看成是一个索引表,所有的英文字符都可以在这个索 ...

  9. SpringBootServletInitializer报错

    1. 现象 从Springboot 1.5.1.RELEASE 升级到Springboot 2.1.2.RELEASE 后SpringBootServletInitializer报错. 2.解决方法 ...

  10. c#mysql数据库备份还原

    1:引用dll MySql.Data.dll,   MySqlbackup.dll 2:建一个数据连接静态类 public static class mysql { public static str ...