HDU 4347 The Closest M Points (kdTree)
赤果果的kdTree。
学习传送门:http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html
其实就是二叉树的变形
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e4+,K = ; #define squ(x) ((x)*(x)) int k,n,idx; struct Point
{
int x[K];
bool operator <(const Point& rhs) const{
return x[idx] < rhs.x[idx];
}
void print() const {
for(int i = ; i < k-; i++)
printf("%d ",x[i]);
printf("%d\n",x[k-]);
}
}P[maxn]; #define fi first
#define se second
typedef pair<double,Point> HeapNode;
priority_queue<HeapNode> q; struct kdTree
{
Point Node[maxn<<];
int son[maxn<<];
#define lch (rt<<1)
#define rch (rt<<1|1) void build(int l,int r,int rt = ,int dep = )
{
if(l>r) return;
son[rt] = r-l;
int x = lch,y = rch;
son[x] = son[y] = -;
idx = dep%k;
int mid = (l+r)>>;
nth_element(P+l,P+mid,P+r+);
Node[rt] = P[mid];
build(l,mid-,x,dep+);
build(mid+,r,y,dep+);
} Point qp; int qm;
void query(int rt = ,int dep = )
{
if(!~son[rt]) return;
HeapNode u(,Node[rt]);
for(int i = ; i < k; i++)
u.fi += squ(u.se.x[i]-qp.x[i]); int dim = dep%k, x = lch, y = rch;
bool flag = false;
if(qp.x[dim]>=Node[rt].x[dim]) swap(x,y);
if(~son[x]) query(x,dep+);
if(q.size()<qm) q.push(u),flag = true;
else {
if(u.fi<q.top().fi) q.pop(),q.push(u);
if(squ(qp.x[dim]-Node[rt].x[dim])<q.top().fi) flag = true;
}
if(flag&&~son[y]) query(y,dep+);
}
}kd; const int M = ;
Point ans[M]; int main()
{
// freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&k)){
for(int i = ; i < n; i++)
for(int j = ; j < k; j++)
scanf("%d",&P[i].x[j]);
kd.build(,n-);
int t; scanf("%d",&t);
while(t--){
for(int i = ; i < k; i++) scanf("%d",&kd.qp.x[i]);
scanf("%d",&kd.qm);
kd.query();
printf("the closest %d points are:\n",kd.qm);
int top = ;
while(q.size()){
ans[++top] = q.top().se;
q.pop();
}
while(top){
ans[top].print();
top--;
}
}
}
return ;
}
HDU 4347 The Closest M Points (kdTree)的更多相关文章
- hdu 4347 The Closest M Points(KD树)
Problem - 4347 一道KNN的题.直接用kd树加上一个暴力更新就撸过去了.写的时候有一个错误就是搜索一边子树的时候返回有当前层数会被改变了,然后就直接判断搜索另一边子树,搞到wa了半天. ...
- 【BZOJ】3053: The Closest M Points(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
- 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 - [KDTree模板题]
本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...
- 数据结构(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 ...
- hdu 4347 The Closest M Points (kd树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4347 题意: 求k维空间中离所给点最近的m个点,并按顺序输出 . 解法: kd树模板题 . 不懂kd树的可以先看看这个 . 不多说, ...
- HDU 1024 Max Sum Plus Plus (动态规划)
HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...
- 【BZOJ3489】A simple rmq problem(KD-Tree)
[BZOJ3489]A simple rmq problem(KD-Tree) 题面 BZOJ 题解 直接做肯定不好做,首先我们知道我们是一个二维平面数点,但是限制区间只能出现一次很不好办,那么我们给 ...
- 【BZOJ1941】Hide and Seek(KD-Tree)
[BZOJ1941]Hide and Seek(KD-Tree) 题面 BZOJ 洛谷 题解 \(KD-Tree\)对于每个点搜一下最近点和最远点就好了 #include<iostream> ...
随机推荐
- HDOJ-1263
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- spring使用过程中遇到的问题
1.出现这样的错误:The type org.springframework.core.NestedRuntimeException cannot be resolved. It is indirec ...
- ECharts使用问题
Echarts官网上给的例子,在最后有一个分号. 使用ajax请求,在eval()转化时出现错误,原因就是因为多了一个分号
- 安装mosquitto报缺少dll文件的错误
解决:下载缺少的dll组件,放到安装目录. 报错:The procedure entry point CRYPTO_memcmp could not be located in the dynami ...
- Python使用Timer实现验证码功能
from threading import Timer import random class Code: def __init__(self): self.make_cache() def make ...
- TypeScript完全解读(26课时)_14.ES6和Nodejs中的模块
创建modules文件夹,我们的文件都写在这里面 modules下面新建index.js文件,在index.ts内引入这个js文件 es6的模块 最主要的两个关键字 import和export imp ...
- 原生JS操作 table object HTMLTableSectionElement 对象,获取行数
<tbody id="infoTab"> <tr class="fomat"> <td class="blank&quo ...
- SmartSql使用教程(3)——SmartSql中的事务,及AOP的使用
一.引言 经过两章的铺垫,我们现在对SmartSql已经有了一定的了解,那么今天我们的主题是事务处理.事务处理是常用的一种特性,而SmartSql至少提供了两种使用事务的方法.一种是通过Reposit ...
- Mac和Unix的常用命令行指令
更新:2017/05/03/02:05 更新: 2017/05/14/11:14 更新: 2017/09/05/16:15 增加rm -rf 强制删除文件夹内所有文件 更新: 2018/01/16 完 ...
- 两个MATLAB在线工具,画图啥的都不用安装了
1. matlab网页版 http://octave-online.net/ http://www.compileonline.com/execute_matlab_online.php 2. 函 ...