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. 【Java】Map转换器

    描述: 在控制层接收参数时候, 往往会出现Json格式需要转换为Bean. 通常一两个字段可以用new去save pojo, 但字段多的情况呢? 以下就是为了解决这个尴尬情况,  自己写一个转换工具类 ...

  2. 1107 Social Clusters (30 分)(并查集)

    并查集的基本应用 #include<bits/stdc++.h> using namespace std; ; vector<int>vec[N]; int p[N]; con ...

  3. Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

  4. ASP.NET MVC5.0 OutputCache不起效果

    按照官网文档(https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing ...

  5. 配置cas可外网访问

    把应用程序tomcat下的conf下的context.xml里配置内容修改 如把: D:\apache-tomcat-APP\conf\context.xml <Resource name=&q ...

  6. [转]juery-zTree的基本用法

    [简介] zTree 是利用 jQuery 的核心代码,实现一套能完成大部分常用功能的 Tree 插件 兼容 IE.FireFox.Chrome 等浏览器 在一个页面内可同时生成多个 Tree 实例 ...

  7. Spring Boot学习(一):入门篇

    目录 Spring Boot简介 Spring Boot快速搭建 1 新建项目 2 运行项目 3 设置spring boot可以热部署(修改后端代码后,自动部署,不用手动部署) 3.1:配置pom.x ...

  8. 【EasyNetQ】- 自动订阅者

    从v0.7.1.30开始,EasyNetQ简单易用AutoSubscriber.你可以用它来轻松地扫描实现任何接口的类的特定组件IConsume<T>或IConsumeAsync<T ...

  9. [剑指Offer] 24.二叉树中和为某一值的路径

    [思路] ·递归先序遍历树, 把结点加入路径. ·若该结点是叶子结点则比较当前路径和是否等于期待和. ·弹出结点,每一轮递归返回到父结点时,当前路径也应该回退一个结点 注:路径定义为从树的根结点开始往 ...

  10. React context基本用法

    React的context就是一个全局变量,可以从根组件跨级别在React的组件中传递.React context的API有两个版本,React16.x之前的是老版本的context,之后的是新版本的 ...