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. MTCNN代码解读

    代码基于bm1682芯片 #include "mtcnn.hpp" #include "utils.hpp" using namespace std; usin ...

  2. Difference between java.lang.RuntimeException and java.lang.Exception

    In Java, there are two types of exceptions: checked exceptions and un-checked exceptions. A checked ...

  3. Re库入门

    1. 正则表达式语法由字符和操作符构成 . 表示任何单个字符   [] 字符集,对单个字符给出取值范围 [abc]表示a.b.c,[a - z]表示a到z单个字符 [^] 非字符集,对单个字符给出排除 ...

  4. 001 SringBoot基础知识及SpringBoot整合Mybatis

    1.原有Spring优缺点分析 (1)优点 Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品.无需开发重量级的Enterprise J ...

  5. LInux基础(04)项目设计一(理解链表管理协议的代码架构)

    要设计好一个项目必须要有一个健全的代码框架 一个结构体内有数据域和处理数据的函数指针, 先实现管理链表的函数 增加节点  删除节点  清空链表  遍历节点对每个节点进行操作 再实现协议的注册 把对象s ...

  6. [开源]Gin + GORM + Casbin+vue-element-admin 实现权限管理系统(golang)

    简析 基于 Gin + GORM + Casbin + vue-element-admin 实现的权限管理系统. 基于Casbin 实现RBAC权限管理. 前端实现: vue-element-admi ...

  7. php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本

    php GD 和图像处理函数,   用  STHUPO.TTF 字体向图像写入文本 注意: 01)   imagettftext() 这个函数不能使用相对路径, 要想使用相对路径要先使用  puten ...

  8. [loj 6253] Yazid的新生舞会

    (很久之前刷的题现在看起来十分陌生a) 题意: 给你一个长度为n的序列A,定义一个区间$[l,r]$是“新生舞会的”当且仅当该区间的众数次数严格大于$\frac{r-l+1}{2}$,求有多少子区间是 ...

  9. 如何配置这个maven仓库的源http://mvnrepository.com/repos

    http://mvnrepository.com/repos 主要是ID .mirrorof.name 怎么配置,这个网站上有spring5.0的,别的仓库没有,我需要这个源. 原文地址:https: ...

  10. asp.net core 3.1 中Synchronous operations are disallowed. Call FlushAsync or set AllowSynchronousIO to true instead

    在Action中解决措施: var syncIOFeature = HttpContext.Features.Get<IHttpBodyControlFeature>(); if (syn ...