Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1235  Solved: 418
[Submit][Status][Discuss]

Description

The course of Software Design and Development Practice is objectionable. ZLC is facing a serious problem .There are many points in K-dimensional space .Given a point. ZLC need to find out the closest m points. Euclidean distance is used as the distance metric between two points. The Euclidean distance between points p and q is the length of the line segment connecting them.In Cartesian coordinates, if p = (p1, p2,..., pn) and q = (q1, q2,..., qn) are two points in Euclidean n-space, then the distance from p to q, or from q to p is given by:
D(p,q)=D(q,p)=sqrt((q1-p1)^2+(q2-p2)^2+(q3-p3)^2…+(qn-pn)^2
Can you help him solve this problem?

软工学院的课程很讨厌!ZLC同志遇到了一个头疼的问题:在K维空间里面有许多的点,对于某些给定的点,ZLC需要找到和它最近的m个点。

(这里的距离指的是欧几里得距离:D(p, q) = D(q, p) =  sqrt((q1 - p1) ^ 2 + (q2 - p2) ^ 2 + (q3 - p3) ^ 2 + ... + (qn - pn) ^ 2)

ZLC要去打Dota,所以就麻烦你帮忙解决一下了……

【Input】

第一行,两个非负整数:点数n(1 <= n <= 50000),和维度数k(1 <= k <= 5)。
接下来的n行,每行k个整数,代表一个点的坐标。
接下来一个正整数:给定的询问数量t(1 <= t <= 10000)
下面2*t行:
  第一行,k个整数:给定点的坐标
  第二行:查询最近的m个点(1 <= m <= 10)

所有坐标的绝对值不超过10000。
有多组数据!

【Output】

对于每个询问,输出m+1行:
第一行:"the closest m points are:" m为查询中的m
接下来m行每行代表一个点,按照从近到远排序。

保证方案唯一,下面这种情况不会出现:
2 2
1 1
3 3
1
2 2
1

Input

In the first line of the text file .there are two non-negative integers n and K. They denote respectively: the number of points, 1 <= n <= 50000, and the number of Dimensions,1 <= K <= 5. In each of the following n lines there is written k integers, representing the coordinates of a point. This followed by a line with one positive integer t, representing the number of queries,1 <= t <=10000.each query contains two lines. The k integers in the first line represent the given point. In the second line, there is one integer m, the number of closest points you should find,1 <= m <=10. The absolute value of all the coordinates will not be more than 10000.
There are multiple test cases. Process to end of file.

Output

For each query, output m+1 lines:
The first line saying :”the closest m points are:” where m is the number of the points.
The following m lines representing m points ,in accordance with the order from near to far
It is guaranteed that the answer can only be formed in one ways. The distances from the given point to all the nearest m+1 points are different. That means input like this:
2 2
1 1
3 3
1
2 2
1
will not exist.

Sample Input

3 2
1 1
1 3
3 4
2
2 3
2
2 3
1

Sample Output

the closest 2 points are:
1 3
3 4
the closest 1 points are:
1 3

HINT

 

Source

 
真正意义上的的K-D Tree
就是把二维扩展到了$k$维
这样只需要在建树的时候按照维度循环建就可以了
 
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 1e6 + , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-')f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, K, WD, root;
int out[MAXN];
struct Point {
int x[];
bool operator < (const Point &rhs) const {
return x[WD] < rhs.x[WD];
}
}P[MAXN], ask;
#define ls(x) T[x].ls
#define rs(x) T[x].rs
struct KDTree {
int mn[], mx[], ls, rs;
Point tp;
}T[MAXN];
struct Ans {
int val, ID;
bool operator < (const Ans &rhs) const{
return val < rhs.val;
}
};
priority_queue<Ans>Q;
int sqr(int x) {
return x * x;
}
void update(int k) {
for(int i = ; i <= K; i++) {
T[k].mn[i] = T[k].mx[i] = T[k].tp.x[i];
if(ls(k)) T[k].mn[i] = min(T[k].mn[i], T[ls(k)].mn[i]), T[k].mx[i] = max(T[k].mx[i], T[ls(k)].mx[i]);
if(rs(k)) T[k].mn[i] = min(T[k].mn[i], T[rs(k)].mn[i]), T[k].mx[i] = max(T[k].mx[i], T[rs(k)].mx[i]);
}
}
int Build(int l, int r, int wd) {
WD = wd;
if(l > r) return ;
int mid = l + r >> ;
nth_element(P + l, P + mid, P + r + );
T[mid].tp = P[mid];
T[mid].ls = Build(l, mid - , (wd + ) % K);
T[mid].rs = Build(mid + , r, (wd + ) % K);
update(mid);
return mid;
}
int GetMinDis(Point a, KDTree b) {
//if(b) return INF;
int ans = ;
for(int i = ; i <= K; i++) {
if(a.x[i] < b.mn[i]) ans += sqr(b.mn[i] - a.x[i]);
if(a.x[i] > b.mx[i]) ans += sqr(a.x[i] - b.mx[i]);
}
return ans;
}
int Dis(Point a, Point b) {
int ans = ;
for(int i = ; i <= K; i++)
ans += sqr(abs(a.x[i] - b.x[i]));
return ans;
}
void Query(int k) {
int ans = Dis(ask, T[k].tp);
if(ans < Q.top().val) Q.pop(), Q.push((Ans){ans, k});
int disl = INF, disr = INF;
if(ls(k)) disl = GetMinDis(ask, T[ls(k)]);
if(rs(k)) disr = GetMinDis(ask, T[rs(k)]);
if(disl < disr) {
if(disl < Q.top().val) Query(ls(k));
if(disr < Q.top().val) Query(rs(k));
}
else {
if(disr < Q.top().val) Query(rs(k));
if(disl < Q.top().val) Query(ls(k));
}
} main() {
while(scanf("%d %d", &N, &K) != EOF) {
for(int i = ; i <= N; i++)
for(int j = ; j <= K; j++)
P[i].x[j] = read();
root = Build(, N, );
int T = read();
while(T--) {
for(int i = ; i <= K; i++) ask.x[i] = read();
int M = read();
printf("the closest %d points are:\n", M);
for(int i = ; i <= M; i++) Q.push((Ans){INF, });
Query(root);
for(int i = ; i <= M; i++)
out[i] = Q.top().ID, Q.pop();
for(int i = M; i >= ; i--)
for(int j = ; j <= K; j++)
printf("%d%c", P[out[i]].x[j], j != K ? ' ' : '\n');
}
}
}
 

BZOJ 3053: The Closest M Points(K-D Tree)的更多相关文章

  1. BZOJ 3053 The Closest M Points

    [题目分析] 典型的KD-Tree例题,求k维空间中的最近点对,只需要在判断的过程中加上一个优先队列,就可以了. [代码] #include <cstdio> #include <c ...

  2. bzoj 3053: The Closest M Points【KD-tree】

    多维KDtree板子 左右儿子的估价用mn~mx当区间,假设区间里的数都存在:k维轮着做割点 #include<iostream> #include<cstdio> #incl ...

  3. 【BZOJ】3053: The Closest M Points(kdtree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...

  4. 【BZOJ】【3053】The Closest M Points

    KD-Tree 题目大意:K维空间内,与给定点欧几里得距离最近的 m 个点. KD树啊……还能怎样啊……然而扩展到k维其实并没多么复杂?除了我已经脑补不出建树过程……不过代码好像变化不大>_&g ...

  5. bzoj 3053 HDU 4347 : The Closest M Points kd树

    bzoj 3053 HDU 4347 : The Closest M Points  kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差 ...

  6. 【BZOJ 3053】The Closest M Points

    KDTree模板,在m维空间中找最近的k个点,用的是欧几里德距离. 理解了好久,昨晚始终不明白那些“估价函数”,后来才知道分情况讨论,≤k还是=k,在当前这一维度距离过线还是不过线,过线则要继续搜索另 ...

  7. The Closest M Points BZOJ 3053

    The Closest M Points [问题描述] 软工学院的课程很讨厌!ZLC同志遇到了一个头疼的问题:在K维空间里面有许多的点,对于某些给定的点,ZLC需要找到和它最近的m个点. (这里的距离 ...

  8. 【kd-tree】bzoj3053 The Closest M Points

    同p2626.由于K比较小,所以不必用堆. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  9. 【HDOJ】4347 The Closest M Points

    居然是KD解. /* 4347 */ #include <iostream> #include <sstream> #include <string> #inclu ...

随机推荐

  1. Git 拉取Gitee仓库报错:“fatal: unable to access ''": Failed to connect to 127.0.0.1 port 1080: Connection refused”

    1.报错信息: 2.本地查看是否Git使用了代理 git config --global http.proxy 3.取消代理 git config --global --unset http.prox ...

  2. LDAP常用命令解析

    OpenLDAP常用命令讲解: ldapadd      -x   进行简单认证      -D   用来绑定服务器的DN      -h   目录服务的地址      -w   绑定DN的密码    ...

  3. 图片(imageView)

    图片(imageView): 常用属性: android:scaleType(图片显示的格式) android:src(图片源,一般使用的资源) android:scaleType属性的常用取值 0. ...

  4. Python 获得汉字笔画

    通过unihan的文件来实现. 只要是unihan中有kTotalStrokes字段,获取其笔画数. Hash也是非常简单清楚的,但想到这些unicode其实会有一个分布规律,就记录了一下, 利用此性 ...

  5. 外贸电商的ERP有很多

    经常有人问我:市面上针对外贸电商的ERP有很多,为什么有的卖家用得风生水起,而我却觉得每款都不合适?一般我都是拒绝回答的,一是因为这种情况大多数跟ERP实施有关,很难用短短几千字就说清楚:二是因为我也 ...

  6. webpack学习笔记丁点积累

    webpack是什么? https://webpack.js.org/concepts/ https://code.tutsplus.com/tutorials/introduction-to-web ...

  7. js变量作用域--变量提升

    1.JS作用域 在ES5中,js只有两种形式的作用域:全局作用域和函数作用域,在ES6中,新增了一个块级作用域(最近的大括号涵盖的范围),但是仅限于let方式申明的变量. 2.变量声明 var x; ...

  8. 转移RMS模拟器

    在PowerShell中识别当前 RMS 模拟器 get-SCOMRMSemulator ?移至另一个管理服务器 –首先将一个新的RMS模拟器管理指定为一个变量 $MS = get-scommanag ...

  9. rabbitmq集群方案

    RabbitMQ的集群方案有以下几种: 1.普通的集群 exchange,buindling再所有的节点上都会保存一份,但是queue只会存储在其中的一个节点上,但是所有的节点都会存储一份queue的 ...

  10. EXP-00032: Non-DBAs may not export other users

    Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit ProductionWith the P ...