【hdu4347】The Closest M Points 【KD树模板】
题意
一个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树模板】的更多相关文章
- bzoj 3053 HDU 4347 : The Closest M Points kd树
bzoj 3053 HDU 4347 : The Closest M Points kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差 ...
- hdu 4347 The Closest M Points (kd树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4347 题意: 求k维空间中离所给点最近的m个点,并按顺序输出 . 解法: kd树模板题 . 不懂kd树的可以先看看这个 . 不多说, ...
- [hdu4347]The Closest M Points(线段树形式kd-tree)
解题关键:kdtree模板题,距离某点最近的m个点. #include<cstdio> #include<cstring> #include<algorithm> ...
- [hdu4347]The Closest M Points(平衡树式kdtree)
解题关键:模板题(结合起来了) #include<iostream> #include<cstdio> #include<cstring> #include< ...
- BZOJ3053:The Closest M Points(K-D Teee)
Description The course of Software Design and Development Practice is objectionable. ZLC is facing a ...
- BZOJ 3053: The Closest M Points(K-D Tree)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1235 Solved: 418[Submit][Status][Discuss] Descripti ...
- k-d树模板(BZOJ2648)
实现了插入一个点,查询距某个位置的最近点. #include <cstdio> #include <algorithm> using namespace std; , inf ...
- tju_4147 kd树+最小生成树
kd树模板+全图最小生成树 标签(空格分隔): kd树+最小生成树 题目链接 题意: k维太空中有n个点,每个点可以与距离它m近的点连边,现在给你一堆点,并给出坐标,现在要建立通信网络,一些可以互相到 ...
- HDU 5992 Finding Hotels(KD树)题解
题意:n家旅店,每个旅店都有坐标x,y,每晚价钱z,m个客人,坐标x,y,钱c,问你每个客人最近且能住进去(非花最少钱)的旅店,一样近的选排名靠前的. 思路:KD树模板题 代码: #include&l ...
随机推荐
- java IO 学习(三)
java IO 学习(一)给了java io 进行分类,这一章学习这些类的常用方法 一.File 1.创建一个新的File的实例: /** * 创建一个新的File实例 */ File f = new ...
- ES6必知必会 (六)—— Class
1.在之前的JS面向对象编程中,如果定义一个构造函数,一般来说是这样: function Person( name , age ) { this.name = name; this.age = age ...
- mongodb数据处理工具
最近大家需要对mongodb和postgres数据库操作比较频繁,给大家推荐一个数据处理工具Kettle,希望对你能有所帮助 ① 将mongodb数据库中的表进行处理然后导出来生成csv,txt, ...
- nexus bower 集成使用
创建nexus bower proxy host 比较简单,如下图: 安装bower && bower-nexus resolver npm install -g bower-nexu ...
- 基于http协议的加密传输方案
最近公司需要通过公网与其它平台完成接口对接,但是基于开发时间和其它因素的考虑,本次对接无法采用https协议实现.既然不能用https协议,那就退而求其次采用http协议吧! 那么问题来了!在对接的过 ...
- centos7上docker安装和使用教程
Docker 是一个创建和管理 Linux 容器的开源工具.容器就像是轻量级的虚拟机,并且可以以毫秒级的速度来启动或停止.Docker 帮助系统管理员和程序员在容器中开发应用程序,并且可以扩展到成千上 ...
- sublime Text 3安装sublimecodeIntel插件
下载:https://github.com/SublimeCodeIntel/SublimeCodeIntel 解压到: data/pacakges目录 安装 Package Control插件 ...
- 智能家居入门DIY——【七、添加一个LCD12864吧】
今天加了一个LCD12864,IC看说明上是ST7567,结果一顿U8g2,发现两个问题: 1.买的时候不知道是卖家写的我理解错了还是怎么了,反正是不出汉字的. 2.U8g2太大了…………占了uno的 ...
- C#操作 iis启用父目录
iis6实现: DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer&quo ...
- VBA7种文档遍历法
Sub 在选定文档最后加入一句话() '遍历文件 Dim MyDialog As FileDialog On Error Resume Next Application.ScreenUpdating ...