题意

一个k维空间,给出n个点的坐标,给出t个询问,每个询问给出一个点的坐标和一个m。对于每个询问找出跟这个点最接近的m个点

分析

kd树的模板题。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std;
typedef long long LL;
const int maxn=;
const int K=;
int num,nownum,m;
LL ans;
struct kdNode{
LL x[K];
int div;
bool lef;
}Ans[];
struct Node{
kdNode a;
LL dis;
bool operator <(const Node &a)const{
return dis<a.dis;
}
Node(){}
Node(kdNode &tmp,LL d){
a=tmp;
dis=d;
}
};
int cmpNo;
bool cmp(kdNode a,kdNode b){
return a.x[cmpNo]<b.x[cmpNo];
}
inline LL max(LL a,LL b){//why?
return a>b?a:b;
}
kdNode p[maxn],q;
LL dis(kdNode a,kdNode b,int k){
LL res=;
for(int i=;i<k;i++){
res+=(a.x[i]-b.x[i])*(a.x[i]-b.x[i]);
}
return res;
}
priority_queue<Node>qq;
void buildKD(int l,int r,kdNode* p,int d,int k){
if(l>r)return ;
int m=(l+r)/;
cmpNo=d;
nth_element(p+l,p+m,p+r+,cmp);
p[m].div=d;
if(l==r){
p[m].lef=;
return ;
}
buildKD(l,m-,p,(d+)%k,k);
buildKD(m+,r,p,(d+)%k,k);
}
void findkd(int l,int r,kdNode& tar,kdNode* p,int k){
if(l>r)return;
int m=(l+r)/;
LL d=dis(p[m],tar,k);
if(p[m].lef){
if(nownum<num){
nownum++;
ans=max(ans,d);
qq.push(Node(p[m],d));
}
else if(ans>d){
qq.pop();
qq.push(Node(p[m],d));
ans=qq.top().dis;
}
return;
}
LL t=tar.x[p[m].div]-p[m].x[p[m].div];
if(t>){
findkd(m+,r,tar,p,k);
if(nownum<num){
qq.push(Node(p[m],d));
nownum++;
ans=qq.top().dis;
findkd(l,m-,tar,p,k);
}else{
if(ans>d){
qq.pop();
qq.push(Node(p[m],d));
ans=qq.top().dis;
}
if(ans>t*t)
findkd(l,m-,tar,p,k);
}
}else{
findkd(l,m-,tar,p,k);
if(nownum<num){
qq.push(Node(p[m],d));
nownum++;
ans=qq.top().dis;
findkd(m+,r,tar,p,k);
}else{
if(ans>d){
qq.pop();
qq.push(Node(p[m],d));
ans=qq.top().dis;
}
if(ans>t*t){
findkd(m+,r,tar,p,k);
}
}
}
} int n,k;
int main(){
while(scanf("%d%d",&n,&k)==){
for(int i=;i<n;i++){
for(int j=;j<k;j++){
scanf("%lld",&p[i].x[j]);
}
p[i].lef=;
}
buildKD(,n-,p,k-,k);
int t;
scanf("%d",&t);
for(int i=;i<=t;i++){
ans=-;
nownum=;
for(int j=;j<k;j++){
scanf("%lld",&q.x[j]);
}
while(!qq.empty())qq.pop();
scanf("%d",&num);
findkd(,n-,q,p,k);
for(int j=;j<=num;j++){
Ans[j]=qq.top().a;
qq.pop();
}
printf("the closest %d points are:\n",num);
for(int j=num;j>=;j--){
for(int kk=;kk<k;kk++){
if(kk==)
printf("%lld",Ans[j].x[kk]);
else
printf(" %lld",Ans[j].x[kk]);
}
printf("\n");
}
}
}
return ;
}

【hdu4347】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树)

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

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

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

  4. [hdu4347]The Closest M Points(平衡树式kdtree)

    解题关键:模板题(结合起来了) #include<iostream> #include<cstdio> #include<cstring> #include< ...

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

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

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

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

  7. k-d树模板(BZOJ2648)

    实现了插入一个点,查询距某个位置的最近点. #include <cstdio> #include <algorithm> using namespace std; , inf ...

  8. tju_4147 kd树+最小生成树

    kd树模板+全图最小生成树 标签(空格分隔): kd树+最小生成树 题目链接 题意: k维太空中有n个点,每个点可以与距离它m近的点连边,现在给你一堆点,并给出坐标,现在要建立通信网络,一些可以互相到 ...

  9. HDU 5992 Finding Hotels(KD树)题解

    题意:n家旅店,每个旅店都有坐标x,y,每晚价钱z,m个客人,坐标x,y,钱c,问你每个客人最近且能住进去(非花最少钱)的旅店,一样近的选排名靠前的. 思路:KD树模板题 代码: #include&l ...

随机推荐

  1. Linux:root下的文件-anaconda-ks.cfg详解

    anaconda-ks.cfg详解 系统安装的时候生成的一个文件,通过这个文件可以修改成自动安装的脚本,用于自动安装同样配置的系统. 自动生成的启动文件anaconda# Kickstart file ...

  2. 内存泄漏 之 MAT工具的使用

    1 内存泄漏的排查方法 Dalvik Debug Monitor Server (DDMS) 是 ADT插件的一部分,其中有两项功能可用于内存检查 : ·    heap 查看堆的分配情况 ·     ...

  3. tornado多路由示例

    main.py代码: # encoding: utf-8 """ @version: ?? @author: andu99 @contact: andux@qq.com ...

  4. iOS语法糖 简单却不那么简单

    转载作者 香蕉大大 (Github) 开发过程中我特别喜欢用语法糖,原因很简单,懒得看到一堆长长的代码,但是语法糖我今天无意中看到更有意思的玩法.这里暂时吧把今天新学到的知识点整理一下希望大家喜欢,如 ...

  5. cool 软件 —— Carnac(实时桌面显示按键)

    1. Carnac 下载地址:Carnac, the Magnificent Keyboard Utility 使用说明:carnac – 在屏幕实时显示按键操作

  6. CIE-LUV是什么颜色特征

    参考文献:维基百科 a simple-to-compute transformation of the 1931 CIE XYZ color space, but which attempted pe ...

  7. .NET 中创建支持集合初始化器的类型

    对象初始化器和集合初始化器只是语法糖,但是能让你的代码看起来更加清晰.至少能让对象初始化的代码和其他业务执行的代码分开,可读性会好一些. 本文将编写一个类型,可以使用集合初始化器构造这个类型.不只是添 ...

  8. cocostudio 使用教程

    项目配置 http://blog.csdn.net/chinahaerbin/article/details/21559351 项目配置如下: 还要引入命名空间之类的: #include " ...

  9. OpenLTE安装教程

    安装需求: USB3 interface Modern multicore CPU (Intel Core i5, Core i7 or equivalent with SSE4.1 SSE4.2 a ...

  10. mediawikide

    wiki.conf server { listen 80; server_name wiki.talkvip.cn; index index.html index.htm index.php; roo ...