【POJ3714】Raid:平面最近点对
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:平面最近点对的更多相关文章
- POJ-3714 Raid 平面最近点对
题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...
- 『Raid 平面最近点对』
平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...
- poj3714 Raid(分治求平面最近点对)
题目链接:https://vjudge.net/problem/POJ-3714 题意:给定两个点集,求最短距离. 思路:在平面最近点对基础上加了个条件,我么不访用f做标记,集合1的f为1,集合2的f ...
- $Poj3714/AcWing\ Raid$ 分治/平面最近点对
$AcWing$ $Sol$ 平面最近点对板子题,注意要求的是两种不同的点之间的距离. $Code$ #include<bits/stdc++.h> #define il inline # ...
- POJ 3741 Raid (平面最近点对)
$ POJ~3741~Raid $ (平面最近点对) $ solution: $ 有两种点,现在求最近的平面点对.这是一道分治板子,但是当时还是想了很久,明明知道有最近平面点对,但还是觉得有点不对劲. ...
- POJ3714 Raid
Raid Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10625 Accepted: 3192 Description ...
- 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点
平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...
- HDU-4631 Sad Love Story 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近 ...
- HDU1007--Quoit Design(平面最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
随机推荐
- SQLServer for linux安装
linux下安装sqlserver数据库有2种办法,第一使用yum镜像安装,第二使用rpm安装包安装 rpm安装地址为:https://packages.microsoft.com/rhel/7/ms ...
- 使用Mysql中的concat函数或正则匹配来快速批量生成用于执行的sql语句
背景介绍 今天需要给一张表里面补数据,需要按照行的维度进行update,如果是个别数据那么直接写update语句就可以了,但是场景要求的是将整表的数据进行update,要实现这个需求就不能只靠蛮力了, ...
- PHP表单select中有0选项的处理
<div class="layui-inline"> <label class="layui-form-label">是否锁定</ ...
- LRU缓存实现
LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高” 在java中可以采用Linke ...
- Python学习之路:列表(List)的append()、extend()与insert()方法
相同点 这三种方法的作用都是为列表(List)添加值 它们的语法为: list.append(obj)list.extend(seq)list.insert(index,obj) #此处index为对 ...
- 同类控件的统一操作(以TCHECKBOX为例)
https://www.cnblogs.com/gaodu2003/archive/2008/12/15/1355355.html Procedure UnCheck; var i: integer; ...
- 【LEETCODE】68、动态规划,medium级别,题目:95、120、91
package y2019.Algorithm.dynamicprogramming.medium; /** * @ProjectName: cutter-point * @Package: y201 ...
- Golang资料集
<Platform-native GUI library for Go> 介绍:跨平台的golang GUI库,支持Windows(xp以上),Unix,Mac OS X(Mac OS X ...
- 使用KONG网关实现接口迁移的灰度验证
在我们对一个API站点进行微服务化的过程中,使用KONG网关可以实现以下几个效果: 1. 业务线无感知,其实内部已经被Kong转到其他站点上执行了,这对业务线特别友好. 2. 可以实现租户级/接口级灰 ...
- Vue 项目 VSCode 调试
调试Vue搭建的前端项目 在项目根目录下的vue.config.js中添加: module.exports = { lintOnSave: false, //关闭eslint语法校验 //填写这部分 ...