传送门

解题思路

  \(KD-Tree\)模板题,\(KD-Tree\)解决的是多维问题,它是一个可以储存\(K\)维数据的二叉树,每一层都被一维所分割。它的插入删除复杂度为\(log^2 n\),它查询最近点对的复杂度为\(O(n^{\frac{k-1}{k}}\),\(k\)代表维数。用堆维护最近点,查询时就先找到它属于的区域,然后回溯时判断一下它到父节点的距离和堆顶的大小,如果比堆顶还大就不递归它的兄弟节点。

代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<queue>
  6. #include<algorithm>
  7. using namespace std;
  8. const int N=50005;
  9. inline int rd(){
  10. int x=0,f=1; char ch=getchar();
  11. while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
  12. while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
  13. return f?x:-x;
  14. }
  15. inline int pw(int x){
  16. return x*x;
  17. }
  18. int n,q,K,t;
  19. struct Node{
  20. int a[7];
  21. void init() {
  22. memset(a,0,sizeof(a));
  23. }
  24. friend bool operator<(const Node A,const Node B){
  25. return A.a[t]<B.a[t];
  26. }
  27. }node[N],pt[N<<2],ans[25];
  28. priority_queue<pair<double,Node> > Q;
  29. struct KD_Tree{
  30. #define mid ((l+r)>>1)
  31. int end[N<<2];
  32. void build(int x,int l,int r,int dep){
  33. if(l>r) return; t=dep%K;
  34. end[x]=0; end[x<<1]=end[x<<1|1]=1;
  35. nth_element(node+l,node+mid,node+r+1);
  36. pt[x]=node[mid];
  37. build(x<<1,l,mid-1,dep+1); build(x<<1|1,mid+1,r,dep+1);
  38. }
  39. void query(int x,int dep,int lim,Node now){
  40. if(end[x]) return;
  41. pair<double,Node> tmp=make_pair(0,pt[x]);
  42. for(int i=0;i<K;i++) tmp.first+=pw(pt[x].a[i]-now.a[i]);
  43. int ls=x<<1,rs=x<<1|1,t=dep%K,flag=0;
  44. if(now.a[t]>=pt[x].a[t]) swap(ls,rs);
  45. if(!end[ls]) query(ls,dep+1,lim,now);
  46. if(Q.size()<lim) Q.push(tmp),flag=1;
  47. else {
  48. if(Q.top().first>tmp.first) Q.pop(),Q.push(tmp);
  49. if(pw(pt[x].a[t]-now.a[t])<Q.top().first) flag=1;
  50. }
  51. if(!end[rs] && flag) query(rs,dep+1,lim,now);
  52. }
  53. #undef mid
  54. }tree;
  55. int main(){
  56. while(~scanf("%d%d",&n,&K)){
  57. for(int i=1;i<=n;i++)
  58. for(int j=0;j<K;j++) node[i].a[j]=rd();
  59. tree.build(1,1,n,0);
  60. for(q=rd();q;q--){
  61. Node now; now.init();
  62. for(int i=0;i<K;i++) now.a[i]=rd();
  63. int t=rd(); tree.query(1,0,t,now);
  64. for(int i=1;!Q.empty();i++)
  65. ans[i]=Q.top().second,Q.pop();
  66. printf("the closest %d points are:\n",t);
  67. for(int i=t;i;i--){
  68. printf("%d",ans[i].a[0]);
  69. for(int j=1;j<K;j++)
  70. printf(" %d",ans[i].a[j]);
  71. putchar('\n');
  72. }
  73. }
  74. }
  75. return 0;
  76. }

hud 4347 The Closest M Points(KD-Tree)的更多相关文章

  1. bzoj 3053 HDU 4347 : The Closest M Points kd树

    bzoj 3053 HDU 4347 : The Closest M Points  kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差 ...

  2. BZOJ 3053: The Closest M Points(K-D Tree)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1235  Solved: 418[Submit][Status][Discuss] Descripti ...

  3. hdu 4347 The Closest M Points (kd树)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4347 题意: 求k维空间中离所给点最近的m个点,并按顺序输出  . 解法: kd树模板题 . 不懂kd树的可以先看看这个 . 不多说, ...

  4. hdu 4347 The Closest M Points(KD树)

    Problem - 4347 一道KNN的题.直接用kd树加上一个暴力更新就撸过去了.写的时候有一个错误就是搜索一边子树的时候返回有当前层数会被改变了,然后就直接判断搜索另一边子树,搞到wa了半天. ...

  5. 数据结构(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 ...

  6. 【HDOJ】4347 The Closest M Points

    居然是KD解. /* 4347 */ #include <iostream> #include <sstream> #include <string> #inclu ...

  7. HDU 4347 - The Closest M Points - [KDTree模板题]

    本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...

  8. BZOJ3053:The Closest M Points(K-D Teee)

    Description The course of Software Design and Development Practice is objectionable. ZLC is facing a ...

  9. HDU 4347 The Closest M Points (kdTree)

    赤果果的kdTree. 学习传送门:http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html 其实就是二叉树的变形 #includ ...

随机推荐

  1. 解决旋转屏幕闪退在androidManifest.template.xml里,activity项添加:

     解决旋转屏幕闪退在androidManifest.template.xml里,activity项添加:android:configChanges="orientation|keyboard ...

  2. 16/7/8_PHP-字符串介绍

    不知道为什么慕课网还这个都要介绍.不过还是一个新的知识点. PHP开发中,我们遇到最多的可能就是字符串. 字符串变量用于包含字符串的值. 一个字符串 通过下面的3种方法来定义: 1.单引号2.双引号3 ...

  3. Mac014--Sourcetree安装(Git client)

    Sourcetree:git client Tool Step1:download address: https://www.sourcetreeapp.com/ Step2:要求username/u ...

  4. [Python3] 012 元组:list,我们不一样!

    目录 0. 元组的独白 1. 元组的创建 2. 元组的特性 (1) 概述 (2) 少废话,上例子 1) 索引 2) 分片 3) 序列运算 4) 成员检测 3. 元组的遍历 (1) 简单总结 (2) 少 ...

  5. centos7下搭建Testlink环境详细过程

    花了半天的时间终于搭建好了完整的Testlink环境,主要包括Mysql以及PHP的版本.未关闭防火墙.以及安装配置过程中遇到的一些问题.以下是详细的搭建过程. 一.工具准备 以下是我在搭建过程中用到 ...

  6. SCUT - 142 - 第n个素数

    https://scut.online/p/142 但是洲阁筛打表还是超时了,打的表不够长吧,在51nod上面要跑5s.要是快10倍得要密1000倍,根本打不出来(时间意义). 暴力check要找的质 ...

  7. td内容超出 以…显示

    table中的td内容超出以省略号显示,需满足的条件是: <style type="text/css"> table{ table-layout: fixed; bor ...

  8. vue防抖节流函数---组件封装,防止按钮多次点击

    1.vue 封装utils.js /** * @param {function} func 执行函数 * @param {number} time 防抖节流时间 * @param {boolean} ...

  9. typescript总结

    1,基础类型 {   布尔值,let isDone:Boolean=true;   数字,let decLiteral:number=true;   字符串,let name:string=" ...

  10. css设置不允许复制文本内容

    之前做一个网上答题的页面时,考虑到要防止考生利用复制粘贴来提高作弊的可能性,就设计了不允许复制.方法也很简单,通过设置CSS 的 user-select就可以达到目的: -moz-user-selec ...