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. AndroidStudio0.5.5发布

    Google%E5%9C%A8%E5%BC%80%E6%BA%90%E4%B8%8A%E7%9A%84%E8%B4%A1%E7%8C%AE http://music.baidu.com/songlis ...

  2. sed-awk命令详解

      第2章 ***********sed***********. 1目  录 2.1 -------sed命令小结及小结图---- 1 2.2 -------第几行---------- 2 2.3 - ...

  3. mongoDB 常用操作CRUD

    1.显示所有的数据库 show dbs   2.切换数据库(如果没有数据库,即是创建数据库) use 数据库名称   3.显示所有的表 show tables   4.查看数据库里的表 show co ...

  4. scp源码浅析

    背景: 经常使用scp传文件,发现它真的很给力,好奇心由来已久! 恰好接到一个移植SSH服务到专有网络(非IP网络)的小任务,完成工作又能满足好奇心,何乐而不为! 我只从源码浅浅的分析一下,后续有更多 ...

  5. User Agent的学习

    什么是User-Agent? User-Agent是一个特殊字符串头,被广泛用来标示浏览器客户端的信息,使得服务器能识别客户机使用的操作系统和版本,CPU类型,浏览器及版本,浏览器的渲染引擎,浏览器语 ...

  6. nginx1.10.3+php5.6+mysql5.7.0

    第一步安装nginx1.10.3 优化nginx的介绍:jemalloc https://ideas.spkcn.com/software/os/linux/577.html 预先安装autoconf ...

  7. 在C/C++程序中打印当前函数调用栈

    前几天帮同事跟踪的一个程序莫名退出,没有core dump(当然ulimit是打开的)的问题.我们知道,正常情况下,如果程序因为某种异常条件退出的话,应该会产生core dump,而如果程序正常退出的 ...

  8. JavaScript五种继承方式详解

    本文抄袭仅供学习http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html 一. 构造函数绑定 ...

  9. Python 类和对象-下

    类的常用函数 #issubclass() 检测一个类是否是另外一个或者一组类中的子类 class Father: pass class Mother: pass class LaoWang: pass ...

  10. Codeforces Round #350 (Div. 2) D1

    D1. Magic Powder - 1 time limit per test 1 second memory limit per test 256 megabytes input standard ...