POJ 3741 Raid (平面最近点对)
$ POJ3741Raid $ (平面最近点对)

$ solution: $
有两种点,现在求最近的平面点对。这是一道分治板子,但是当时还是想了很久,明明知道有最近平面点对,但还是觉得有点不对劲。基本算法专题出最近平面点对?怎么感觉我 $ Noip $ 凉了? 这题不会是个坑吧。。。。
嗯,不瞎扯了。来回顾一下分治求平面点对的过程,首先将点按横坐标排序,然后整个区间不断往下二分,回溯的时候归并排序(这其实我来再写一次题解的原因,以前写的都是快排,但必须承认归并的复杂度才是最稳最准的)。我们将两个区间合并时,从中间点想外扩 $ d $ 的距离,这个距离内的点才有可能更新答案,而且这个距离里的每个点都不会计算多次,这个是有证明的 。然后我们暴力计算即可。
$ code: $
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define db double
#define rg register int
using namespace std;
int t,n;
struct su{
db x,y;
bool z;
inline bool operator <(const su &yy){
return x<yy.x;
}
}a[200005],b[200005];
inline int qr(){
register char ch; register bool sign=0; rg res=0;
while(!isdigit(ch=getchar()))if(ch=='-')sign=1;
while(isdigit(ch))res=res*10+(ch^48),ch=getchar();
if(sign)return -res; else return res;
}
inline db dis(const su &x,const su &y){
return pow((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y),0.5);
}
inline db ask(int sl,int sr){
if(sl==sr)return 1e9;
rg mid=(sl+sr)>>1;
db d=min(ask(sl,mid),ask(mid+1,sr));
rg l=sl,r=mid+1,tt=sl;
while(l<=mid&&r<=sr)
if(a[l].y<a[r].y)b[tt++]=a[l++];
else b[tt++]=a[r++];
while(l<=mid)b[tt++]=a[l++];
while(r<=sr)b[tt++]=a[r++];
for(rg i=r=sl;i<tt;++i){ a[i]=b[i];
if(fabs(b[i].x-b[mid].x)>d)continue;
while(r<=sr&&b[r].y-b[l].y<=d)++r;
for(rg j=i+1;j<r;++j)
if(b[j].z!=b[i].z&&fabs(b[j].x-b[mid].x)<=d)//这里不能少
d=min(d,dis(b[i],b[j]));
}return d;
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
t=qr();
while(t--){
n=qr();
for(rg i=1;i<=n;++i)
a[i].x=qr(),a[i].y=qr(),a[i].z=1;
for(rg i=n+1;i<=n<<1;++i)
a[i].x=qr(),a[i].y=qr();
sort(a+1,a+2*n+1);
printf("%.3lf\n",ask(1,2*n));
}
return 0;
}
POJ 3741 Raid (平面最近点对)的更多相关文章
- 『Raid 平面最近点对』
平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...
- POJ-3714 Raid 平面最近点对
题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...
- 【POJ3714】Raid:平面最近点对
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- 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 3714 Raid
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- 计算几何 平面最近点对 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 ...
随机推荐
- 2018 icpc 沈阳
https://codeforces.com/gym/101955 J 签到 #include<iostream> #include<cstring> #include< ...
- centos官网镜像下载方法
1.CentoS简介 CentOS(Community Enterprise Operating System,社区企业操作系统)是一个基于Red Hat Linux 提供的可自由使用源代码的企业级L ...
- JS单引号嵌套的问题,怎么改才能对呢!
JS单引号嵌套的问题,怎么改才能对呢! https://zhidao.baidu.com/question/416584343.html document.getElementById(celbid) ...
- docker nginx ssl
docker run -d -p 80:80 -p 443:443 -v $(pwd)/conf/conf.d:/etc/nginx/conf.d -v $(pwd)/cert:/etc/nginx/ ...
- 【HANA系列】SAP HANA SQL获取本周的周一
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL获取本周 ...
- 【DSP开发】【Linux开发】基于ARM+DSP进行应用开发
针对当前应用的复杂性,SOC芯片更好能能满足应用和媒体的需求,集成众多接口,用ARM做为应用处理器进行多样化的应用开发和用户界面和接口,利用DSP进行算法加速,特别是媒体的编解码算法加速,既能够保持算 ...
- 使用bat脚本进行开机启动批处理
@echo off//关闭提示信息 start /b "F:\newProject\project\Kinect\Kinect\bin\Debug\" /min Kinect.ex ...
- ConfigurationManager.ConnectionStrings 属性
public static ConnectionStringSettingsCollection ConnectionStrings { get; } App.config <?xml vers ...
- Maven项目的常用jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 2019.07.06 纪中_B
今天的题看起来都很简单,结果就爆炸了 做题时:怎么都是图论??? 结果最后好多是DP(最恐怖的是还有我没学过的状压DP) 2019.07.06[NOIP提高组]模拟 B 组 做了两题(稍微腐败了一下) ...