hud 4347 The Closest M Points(KD-Tree)
解题思路
\(KD-Tree\)模板题,\(KD-Tree\)解决的是多维问题,它是一个可以储存\(K\)维数据的二叉树,每一层都被一维所分割。它的插入删除复杂度为\(log^2 n\),它查询最近点对的复杂度为\(O(n^{\frac{k-1}{k}}\),\(k\)代表维数。用堆维护最近点,查询时就先找到它属于的区域,然后回溯时判断一下它到父节点的距离和堆顶的大小,如果比堆顶还大就不递归它的兄弟节点。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
const int N=50005;
inline int rd(){
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
}
inline int pw(int x){
return x*x;
}
int n,q,K,t;
struct Node{
int a[7];
void init() {
memset(a,0,sizeof(a));
}
friend bool operator<(const Node A,const Node B){
return A.a[t]<B.a[t];
}
}node[N],pt[N<<2],ans[25];
priority_queue<pair<double,Node> > Q;
struct KD_Tree{
#define mid ((l+r)>>1)
int end[N<<2];
void build(int x,int l,int r,int dep){
if(l>r) return; t=dep%K;
end[x]=0; end[x<<1]=end[x<<1|1]=1;
nth_element(node+l,node+mid,node+r+1);
pt[x]=node[mid];
build(x<<1,l,mid-1,dep+1); build(x<<1|1,mid+1,r,dep+1);
}
void query(int x,int dep,int lim,Node now){
if(end[x]) return;
pair<double,Node> tmp=make_pair(0,pt[x]);
for(int i=0;i<K;i++) tmp.first+=pw(pt[x].a[i]-now.a[i]);
int ls=x<<1,rs=x<<1|1,t=dep%K,flag=0;
if(now.a[t]>=pt[x].a[t]) swap(ls,rs);
if(!end[ls]) query(ls,dep+1,lim,now);
if(Q.size()<lim) Q.push(tmp),flag=1;
else {
if(Q.top().first>tmp.first) Q.pop(),Q.push(tmp);
if(pw(pt[x].a[t]-now.a[t])<Q.top().first) flag=1;
}
if(!end[rs] && flag) query(rs,dep+1,lim,now);
}
#undef mid
}tree;
int main(){
while(~scanf("%d%d",&n,&K)){
for(int i=1;i<=n;i++)
for(int j=0;j<K;j++) node[i].a[j]=rd();
tree.build(1,1,n,0);
for(q=rd();q;q--){
Node now; now.init();
for(int i=0;i<K;i++) now.a[i]=rd();
int t=rd(); tree.query(1,0,t,now);
for(int i=1;!Q.empty();i++)
ans[i]=Q.top().second,Q.pop();
printf("the closest %d points are:\n",t);
for(int i=t;i;i--){
printf("%d",ans[i].a[0]);
for(int j=1;j<K;j++)
printf(" %d",ans[i].a[j]);
putchar('\n');
}
}
}
return 0;
}
hud 4347 The Closest M Points(KD-Tree)的更多相关文章
- bzoj 3053 HDU 4347 : The Closest M Points kd树
bzoj 3053 HDU 4347 : The Closest M Points kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差 ...
- BZOJ 3053: The Closest M Points(K-D Tree)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1235 Solved: 418[Submit][Status][Discuss] Descripti ...
- hdu 4347 The Closest M Points (kd树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4347 题意: 求k维空间中离所给点最近的m个点,并按顺序输出 . 解法: kd树模板题 . 不懂kd树的可以先看看这个 . 不多说, ...
- hdu 4347 The Closest M Points(KD树)
Problem - 4347 一道KNN的题.直接用kd树加上一个暴力更新就撸过去了.写的时候有一个错误就是搜索一边子树的时候返回有当前层数会被改变了,然后就直接判断搜索另一边子树,搞到wa了半天. ...
- 数据结构(KD树):HDU 4347 The Closest M Points
The Closest M Points Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Ot ...
- 【HDOJ】4347 The Closest M Points
居然是KD解. /* 4347 */ #include <iostream> #include <sstream> #include <string> #inclu ...
- HDU 4347 - The Closest M Points - [KDTree模板题]
本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...
- BZOJ3053:The Closest M Points(K-D Teee)
Description The course of Software Design and Development Practice is objectionable. ZLC is facing a ...
- HDU 4347 The Closest M Points (kdTree)
赤果果的kdTree. 学习传送门:http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html 其实就是二叉树的变形 #includ ...
随机推荐
- 解决旋转屏幕闪退在androidManifest.template.xml里,activity项添加:
解决旋转屏幕闪退在androidManifest.template.xml里,activity项添加:android:configChanges="orientation|keyboard ...
- 16/7/8_PHP-字符串介绍
不知道为什么慕课网还这个都要介绍.不过还是一个新的知识点. PHP开发中,我们遇到最多的可能就是字符串. 字符串变量用于包含字符串的值. 一个字符串 通过下面的3种方法来定义: 1.单引号2.双引号3 ...
- Mac014--Sourcetree安装(Git client)
Sourcetree:git client Tool Step1:download address: https://www.sourcetreeapp.com/ Step2:要求username/u ...
- [Python3] 012 元组:list,我们不一样!
目录 0. 元组的独白 1. 元组的创建 2. 元组的特性 (1) 概述 (2) 少废话,上例子 1) 索引 2) 分片 3) 序列运算 4) 成员检测 3. 元组的遍历 (1) 简单总结 (2) 少 ...
- centos7下搭建Testlink环境详细过程
花了半天的时间终于搭建好了完整的Testlink环境,主要包括Mysql以及PHP的版本.未关闭防火墙.以及安装配置过程中遇到的一些问题.以下是详细的搭建过程. 一.工具准备 以下是我在搭建过程中用到 ...
- SCUT - 142 - 第n个素数
https://scut.online/p/142 但是洲阁筛打表还是超时了,打的表不够长吧,在51nod上面要跑5s.要是快10倍得要密1000倍,根本打不出来(时间意义). 暴力check要找的质 ...
- td内容超出 以…显示
table中的td内容超出以省略号显示,需满足的条件是: <style type="text/css"> table{ table-layout: fixed; bor ...
- vue防抖节流函数---组件封装,防止按钮多次点击
1.vue 封装utils.js /** * @param {function} func 执行函数 * @param {number} time 防抖节流时间 * @param {boolean} ...
- typescript总结
1,基础类型 { 布尔值,let isDone:Boolean=true; 数字,let decLiteral:number=true; 字符串,let name:string=" ...
- css设置不允许复制文本内容
之前做一个网上答题的页面时,考虑到要防止考生利用复制粘贴来提高作弊的可能性,就设计了不允许复制.方法也很简单,通过设置CSS 的 user-select就可以达到目的: -moz-user-selec ...