版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 4347

题意:

  求k维空间中离所给点最近的m个点,并按顺序输出  。

解法:

  kd树模板题 。

  不懂kd树的可以先看看这个

  不多说,上代码 。

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#define ll long long using namespace std; const int N=;
const int K=; int n,m; struct point{
int a[K];
int div; // 按哪个维度划分
bool lef; // 是否是叶子节点
ll dis; // 离询问点的距离。注意这个在读入建树时不会用到,在进入队列时才用到
void print(){
printf("%d",a[]);
for (int i=;i<m;i++)
printf(" %d",a[i]);
puts("");
}
bool operator < (const point &t) const{
return dis<t.dis;
}
point(){}
point(point &t,ll d){
for (int i=;i<m;i++) a[i]=t.a[i];
dis=d;
}
}p[N],tar; int cmp_NO;
inline bool cmp(point x,point y){
return x.a[cmp_NO]<y.a[cmp_NO];
} inline ll dis(point x,point y){
ll ret=;
for (int i=;i<m;i++)
ret+=(x.a[i]-y.a[i])*(x.a[i]-y.a[i]);
return ret;
} inline void bulid_kdt(int L,int R,int d){
if (L>R) return;
int mid=(L+R)>>;
cmp_NO=d;
nth_element(p+L,p+mid,p+R+,cmp);
p[mid].div=d;
if (L==R){
p[L].lef=true;
return;
}
bulid_kdt(L,mid-,(d+)%m);
bulid_kdt(mid+,R,(d+)%m);
} priority_queue<point> que;
int num,nownum;
ll ans; inline void find_kd(int L,int R){
if (L>R) return; int mid=(L+R)>>;
ll d=dis(p[mid],tar);
if (p[mid].lef){
if (nownum<num){
nownum++;
que.push(point(p[mid],d));
ans=max(ans,d);
}
else if (ans>d){
que.pop();
que.push(point(p[mid],d));
ans=que.top().dis;
}
return;
} int t=tar.a[p[mid].div]-p[mid].a[p[mid].div];
if (t>){
find_kd(mid+,R);
if (nownum<num){
nownum++;
que.push(point(p[mid],d));
ans=max(ans,d);
find_kd(L,mid-);
}
else {
if (ans>d){
que.pop();
que.push(point(p[mid],d));
ans=que.top().dis;
}
if (ans>t*t)
find_kd(L,mid-);
}
}
else {
find_kd(L,mid-);
if (nownum<num){
nownum++;
que.push(point(p[mid],d));
ans=max(ans,d);
find_kd(mid+,R);
}
else{
if (ans>d){
que.pop();
que.push(point(p[mid],d));
ans=que.top().dis;
}
if (ans>t*t)
find_kd(mid+,R);
}
}
} inline void put(){
if (que.empty()) return;
point pp=que.top();
que.pop();
put();
pp.print();
} int main(){
while (~scanf("%d%d",&n,&m)){
for (int i=;i<n;i++){
for (int j=;j<m;j++)
scanf("%d",&p[i].a[j]);
p[i].lef=false;
} bulid_kdt(,n-,); // 这一步相当于将原数组重新排了个序,先访问到的点放在中间 int q;
scanf("%d",&q);
while (q--){
for (int i=;i<m;i++)
scanf("%d",&tar.a[i]);
while (!que.empty()) que.pop();
scanf("%d",&num);
nownum=;
ans=-;
find_kd(,n-);
printf("the closest %d points are:\n",num);
put();
}
}
return ;
}

hdu 4347 The Closest M Points (kd树)的更多相关文章

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

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

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

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

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

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

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

  5. HDU 4347 The Closest M Points (kdTree)

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

  6. 【HDOJ】4347 The Closest M Points

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

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

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

  8. [hdu4347]The Closest M Points(线段树形式kd-tree)

    解题关键:kdtree模板题,距离某点最近的m个点. #include<cstdio> #include<cstring> #include<algorithm> ...

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

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

随机推荐

  1. 【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT

    题目描述 给你 $n$ 个点,支持 $m$ 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. $n\le 3\times 10^5$ ,$ ...

  2. python基础(5)

    使用dict和set dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子 ...

  3. C++11新利器

    C++11常用特性的使用经验总结 unordered_map可能用的会比较多 省的写哈希表了. 但是浪费空间

  4. Linux用户创建及权限管理

    作业一: 1,新建用户natasha,uid为1000,gid为555,备注信息为“master” useradd natasha            vim /etc/passwd         ...

  5. Oracle中用exp/imp命令参数详解【转】

    Oracle中用exp/imp命令参数详解 [用 exp 数 据 导 出]:1  将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中   exp sy ...

  6. 手机 safari mac 调试

    1.下载 safari technology preview 2.iphone 设置: 打开“设置”程序,进入“Safari”->“高级”页面开启“Web检查器” 3.mac 上打开 safar ...

  7. php 修改图片分辨率

    <?php function resize_image($file, $w, $h, $crop=FALSE) { list($width, $height) = getimagesize($f ...

  8. 【题解】【LibreOJ Beta Round #5】游戏 LOJ 531 基环树 博弈论

    Prelude 题目链接:萌萌哒传送门♪(^∇^*) Subtask 1 & 2 这是什么鬼题面... 首先要看出,这就是一个基环树博弈. 具体题意:给出一个基环内向树,一个棋子初始在\(1\ ...

  9. CountUp.js让页面数字跳动起来

    CountUp.js 无依赖的.轻量级的 JavaScript 类,可以用来快速创建以一种更有趣的动画方式显示数值数据.尽管它的名字叫 countUp,但其实可以在两个方向进行变化,这是根据你传递的 ...

  10. ssl证书生成方法

    一般情况下,如果能找到可用的证书,就可以直接使用,只不过会因证书的某些信息不正确或与部署证书的主机不匹配而导致浏览器提示证书无效,但这并不影响使用. 需要手工生成证书的情况有: 找不到可用的证书 需要 ...