poj3714 Raid(分治求平面最近点对)
题目链接:https://vjudge.net/problem/POJ-3714
题意:给定两个点集,求最短距离。
思路:在平面最近点对基础上加了个条件,我么不访用f做标记,集合1的f为1,集合2的f为-1,那么求两个点的距离时,如果a.f*b.f=-1时计算距离,否则乘积为1的话返回inf。其它就和hdoj1007一样了.
AC代码:
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std; const int maxn=2e5+;
const double inf=1e30;
int T,n,cnt,id[maxn];
struct node{
int x,y,f;
}pt[maxn]; bool operator < (const node& a,const node& b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
} bool cmp(int a,int b){
return pt[a].y<pt[b].y;
} double dist(const node& a,const node& b){
if(a.f*b.f==) return inf;
return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
} double fenzhi(int l,int r){
double d=inf;
if(l==r) return d;
if(l+==r) return dist(pt[l],pt[r]);
int mid=(l+r)>>;
d=min(fenzhi(l,mid),fenzhi(mid+,r));
cnt=;
int t1,t2,l1=l,r1=mid,mid1;
while(l1<=r1){
mid1=(l1+r1)>>;
if(pt[mid].x-pt[mid1].x<d) r1=mid1-;
else l1=mid1+;
}
t1=l1;
l1=mid+,r1=r;
while(l1<=r1){
mid1=(l1+r1)>>;
if(pt[mid1].x-pt[mid].x<d) l1=mid1+;
else r1=mid1-;
}
t2=r1;
for(int i=t1;i<=t2;++i)
id[++cnt]=i;
sort(id+,id+cnt+,cmp);
for(int i=;i<cnt;++i)
for(int j=i+;j<=cnt&&(pt[id[j]].y-pt[id[i]].y<d);++j)
d=min(d,dist(pt[id[i]],pt[id[j]]));
return d;
} int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<=*n;++i){
scanf("%d%d",&pt[i].x,&pt[i].y);
if(i<=n) pt[i].f=;
else pt[i].f=-;
}
sort(pt+,pt++*n);
printf("%.3f\n",fenzhi(,*n));
}
return ;
}
poj3714 Raid(分治求平面最近点对)的更多相关文章
- POJ 3714 分治/求平面最近点对
第一次见这种问题直接懵圈...没想到分治法这么强大,借鉴了lyd的代码: 代码如下 #include<cstdio> #include<algorithm> #include& ...
- POJ3714 Raid 分治/K-D Tree
VJ传送门 简要题意:给出两个大小均为\(N\)的点集\(A,B\),试在\(A\)中选择一个点,在\(B\)中选择一个点,使得它们在所有可能的选择方案中欧几里得距离最小,求出这个距离 下面给出的两种 ...
- (模板)hdoj1007(分治求平面最小点对)
题目链接:https://vjudge.net/problem/HDU-1007 题意:给定n个点,求平面距离最小点对的距离除2. 思路:分治求最小点对,对区间[l,r]递归求[l,mid]和[mid ...
- Vijos 1012 清帝之惑之雍正 平面最近点对(分治)
背景 雍正帝胤祯,生于康熙十七年(1678)是康熙的第四子.康熙61年,45岁的胤祯继承帝位,在位13年,死于圆明园.庙号世宗. 胤祯是在康乾盛世前期--康熙末年社会出现停滞的形式下登上历史舞台的.复 ...
- wannafly 练习赛11 E 求最值(平面最近点对)
链接:https://www.nowcoder.com/acm/contest/59/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit ...
- HDU1007--Quoit Design(平面最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- hdu 1007 Quoit Design(平面最近点对)
题意:求平面最近点对之间的距离 解:首先可以想到枚举的方法,枚举i,枚举j算点i和点j之间的距离,时间复杂度O(n2). 如果采用分治的思想,如果我们知道左半边点对答案d1,和右半边点的答案d2,如何 ...
- HDU-4631 Sad Love Story 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近 ...
- $Poj3714/AcWing\ Raid$ 分治/平面最近点对
$AcWing$ $Sol$ 平面最近点对板子题,注意要求的是两种不同的点之间的距离. $Code$ #include<bits/stdc++.h> #define il inline # ...
随机推荐
- (五)CWnd 所有窗口类的父类,CFrameWnd,Afx_xxx 全局函数,命名规范
CWnd::MessageBox: 只有CWnd的派生类才可以使用MessageBox 所以应用程序类中使用:AfxMessageBox // 初始化 OLE 库 if (!AfxOleInit()) ...
- use potato
- js监听页面标签切换
var OriginTitile = document.title, titleTime; document.addEventListener('visibilitychange', function ...
- 2 - Rich feature hierarchies for accurate object detection and semantic segmentation(阅读翻译)
Rich feature hierarchies for accurate object detection and semantic segmentation Ross Girshick Jeff ...
- ps 证件照制作
自己制作证件照,再通过印鸽等服务打印邮寄,个人感觉还是比较方便实惠. 使用ps的定义图案和填充功能(ps精简版) 定义图案 1,打开1寸照片 2,图像=>图像大小,可选去掉约束比例 1寸:2.5 ...
- ElasticSearch1:基本概念
ElasticSearch的基本概念 es基本概念: Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档,用JSON作为文档序列化的格式 NRT:Nearly Real Time ...
- antd源码分析之——标签页(tabs 3.Tabs的滚动效果)
由于ant Tabs组件结构较复杂,共分三部分叙述,本文为目录中第三部分(高亮) 目录 一.组件结构 antd代码结构 rc-ant代码结构 1.组件树状结构 2.Context使用说明 3.rc-t ...
- curl 使用笔记
一.使用案例 curl -H "cookie:userName=shangyy" www.baidu.com 二.使用 1.从Netscape的网页服务器上获得该网站的主页: cu ...
- SpringBoot几种定时任务的实现方式 和多线程执行任务
定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...
- 链接Linux工具(SecureCRT)
SecureCRT下载 点我下载 http://download.csdn.net/download/weixin_39549656/10207279 安装 先运行注册机 链接 输入密码 出现以下界面 ...