K-D树可以看看这个博客写的真心不错!这里存个版

http://blog.csdn.net/zhjchengfeng5/article/details/7855241

HDU 4349

#include <map>
//KD树学习http://blog.csdn.net/zhjchengfeng5/article/details/7855241
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 100010
const LL INF = LONG_LONG_MAX;
struct node
{
LL pos[10];
int id;
}tree[MAXN],op,point;
int split[MAXN],N,now,demension;
bool used[MAXN];
LL ans,id;
double var[10];
bool cmp(const node &a,const node &b)
{
return a.pos[split[now]] < b.pos[split[now]];
}
void build(int L,int R)
{
if (L > R) return;
int mid = (L + R) / 2;
//求出每一唯上的方差
for (int pos = 0 ; pos < demension ; pos++)
{
double avg = var[pos] = 0;
for (int i = L ; i <= R; i++)
avg += tree[i].pos[pos];
avg /= (R - L + 1);
for (int i = L ; i <= R; i++)
var[pos] += (tree[i].pos[pos] - avg) * (tree[i].pos[pos] - avg);
var[pos] /= (R - L + 1);
}
//找到方差最大的那一个唯独
split[now = mid] = 0;
for (int i = 1; i < demension ; i++)
if (var[split[mid]] < var[i]) split[mid] = i;
nth_element(tree + L ,tree + mid,tree + R + 1,cmp);
build(L,mid - 1);
build(mid + 1,R);
}
void query(int L,int R)
{
if (L > R) return;
int mid = (L + R) / 2;
LL dis = 0;
//求出目标点到当前根节点的距
for (int i = 0 ; i < demension ; i++)
dis += (op.pos[i] - tree[mid].pos[i]) * (op.pos[i] - tree[mid].pos[i]);
//if (dis == 0) dis = INF;
//printf("%lld\n",dis);
//如果当前节点能够用来更新最近距离并且dis<ans
if (!used[tree[mid].id] && dis < ans)
{
ans = dis;
id = tree[mid].id;
point = tree[mid];
}
//计算op到分裂平面的距离
LL radius = (op.pos[split[mid]] - tree[mid].pos[split[mid]]) *
(op.pos[split[mid]] - tree[mid].pos[split[mid]]);
//对子区间进行查询
if (op.pos[split[mid]] < tree[mid].pos[split[mid]])
{
query(L,mid - 1);
if (radius <= ans) query(mid + 1,R);
}
else
{
query(mid + 1,R);
if (radius <= ans) query(L,mid - 1);
}
}
node ret[20];
int main()
{
// freopen("sample.txt","r",stdin);
while (scanf("%d%d",&N,&demension) != EOF)
{
for (int i = 1; i <= N; i++)
for (int j = 0 ;j < demension ; j++)
scanf("%lld",&tree[i].pos[j]);
for (int i = 1; i <= N; i++) tree[i].id = i;
build(1,N);
int T;
scanf("%d",&T);
for (int i = 1; i <= T; i++)
{
for (int j = 0; j < demension ; j++)
scanf("%lld",&op.pos[j]);
//printf("%lld %lld\n",op.pos[0],op.pos[1]);
memset(used,false,sizeof(used));
int m;
scanf("%d",&m);
for (int j = 0 ; j < m ; j++)
{
ans = INF;
query(1,N);
ret[j] = point;
//printf("%lld\n",id);
used[id] = true;
}
printf("the closest %d points are:\n",m);
for (int j = 0 ; j < m ; j++)
{
printf("%lld",ret[j].pos[0]);
for (int k = 1; k < demension ; k++)
printf(" %lld",ret[j].pos[k]);
putchar('\n');
}
}
}
return 0;
}

  

K-D树问题 HDU 4347的更多相关文章

  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. HDU 5412 CRB and Queries(区间第K大 树套树 按值建树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5412 Problem Description There are N boys in CodeLan ...

  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. HDU 4347 - The Closest M Points - [KDTree模板题]

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

  7. 【线段树】HDU 5493 Queue (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5493 题目大意: N个人,每个人有一个唯一的高度h,还有一个排名r,表示它前面或后面比它高的人的个数 ...

  8. 【线段树】HDU 5443 The Water Problem

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 题目大意: T组数据.n个值,m个询问,求区间l到r里的最大值.(n,m<=1000) ...

  9. 狂K 线段树

    想写好树剖~~线段树very important HDU 1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

随机推荐

  1. sphinx调用API参考(官方手册)

    API的参考实现是用PHP写成的,因为(我们相信)较之其他语言,Sphinx在PHP中应用最广泛.因此这份参考文档基于PHP API的参考,而且这节中的所有的代码样例都用PHP给出. 当然,其他所有A ...

  2. docker容器中启动kvm虚拟机

    .安装docker yum install docker systemctl start docker.service systemctl enable docker.service .拉取cento ...

  3. Alpha阶段展示

    程序员杀产品经理祭天(SacrificePM)团队 1.团队成员简介和个人博客地址 故事 我们队伍的建立过程稍具戏剧性,大家看我们也颇为奇怪,这么一支8人队伍是怎么诞生的呢?其实我们原本分属三组,而第 ...

  4. get? post? put? delete? head? trace? options? http请求方法

    http1.1协议里面定义了八种请求方法: get:用作获取,读取数据 post:向指定的资源提交数据 put:更新,向指定的资源上传一个内容,比如说:更新一个用户的头像或者替换掉已有的一个视频 de ...

  5. Qscintilla2编译使用

    Qscintilla2的下载地址: https://github.com/josephwilk/qscintilla https://riverbankcomputing.com/software/q ...

  6. 安装并配置maven

    1下载Maven 2添加仓库(仓库就是maven项目统一存放依赖的地方 根据groupId ArtifactId Version来组成项目依赖路径) conf——settings.xml------- ...

  7. Isolate-user-vlan技术白皮书

    http://www.h3c.com.cn/Products___Technology/Technology/LAN/Other_technology/Technology_book/200804/6 ...

  8. chm文件空白如何解决

    解决办法:http://jingyan.baidu.com/article/8275fc86b5fb6646a03cf6b0.html

  9. c# 调用 matlab 引发初始化错误 异常

    1. 除了matlab 编译的DLL 意外还需要引用 MWArray.dll 这个dill 在安装了  MCRInstaller.exe(matlab运行环境之后就会有了): 2. 最重要的一点.ne ...

  10. 【题解】HAOI2008木棍分割

    对于这道题目的两问,第一问直接二分答案求出最短长度.关键在于第二问应当如何求:建立dp方程,dp[i][j]代表到第i个分界线,切了j次(强制在第i处切一刀.这样就不会对后面的状态产生影响).状态转移 ...