数据结构(KD树):HDU 4347 The Closest M Points

The Closest M Points

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)
Total Submission(s): 3285    Accepted Submission(s): 1201

Problem 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:

Can you help him solve this problem?

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

  

  绳命中第一道KD树,模板题,照着打的。

  难道————KD树==剪枝?嗯,我再想想~

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <queue>
 6 using namespace std;
 7 const int maxn=200010;
 8 int cmpNo,K;
 9 struct Node{
10     int x[10],l,r,id;
11     bool operator <(const Node &b)const{
12         return x[cmpNo]<b.x[cmpNo];
13     }
14 };
15
16 long long Dis(const Node &a,const Node &b){
17     long long ret=0;
18     for(int i=0;i<K;i++)
19         ret+=(a.x[i]-b.x[i])*(a.x[i]-b.x[i]);
20     return ret;
21 }
22
23 Node p[maxn];
24
25 int Build(int l,int r,int d){
26     if(l>r)return 0;
27     cmpNo=d;
28     int mid=l+r>>1;
29     nth_element(p+l,p+mid,p+r+1);
30     p[mid].l=Build(l,mid-1,(d+1)%K);
31     p[mid].r=Build(mid+1,r,(d+1)%K);
32     return mid;
33 }
34
35 priority_queue<pair<long long,int> >q;
36 void Kth(int l,int r,Node tar,int k,int d){
37     if(l>r)return;
38     int mid=l+r>>1;
39     pair<long long,int>v=make_pair(Dis(p[mid],tar),p[mid].id);
40     if(q.size()==k&&v<q.top())q.pop();
41     if(q.size()<k)q.push(v);
42     long long t=tar.x[d]-p[mid].x[d];
43     if(t<=0){
44         Kth(l,mid-1,tar,k,(d+1)%K);
45         if(q.top().first>t*t)
46             Kth(mid+1,r,tar,k,(d+1)%K);
47     }
48     else{
49         Kth(mid+1,r,tar,k,(d+1)%K);
50         if(q.top().first>t*t)
51             Kth(l,mid-1,tar,k,(d+1)%K);
52     }
53 }
54 int k,ans[20];
55 Node a[maxn];
56 int main(){
57     int n;
58     while(scanf("%d%d",&n,&K)!=EOF){
59         for(int id=1;id<=n;id++){
60             for(int i=0;i<K;i++)
61                 scanf("%d",&p[id].x[i]);
62             p[id].id=id;
63             a[id]=p[id];
64         }
65         Build(1,n,0);
66         int Q;
67         scanf("%d",&Q);
68         Node tar;
69         while(Q--){
70             for(int i=0;i<K;i++)
71                 scanf("%d",&tar.x[i]);
72             scanf("%d",&k);
73             Kth(1,n,tar,k,0);
74             printf("the closest %d points are:\n",k);
75             int tot=0;
76             while(!q.empty()){
77                 int id=(q.top()).second;q.pop();
78                 ans[tot++]=id;
79             }
80             for(int i=tot-1;i>=0;i--)
81                 for(int j=0;j<K;j++)
82                     printf("%d%c",a[ans[i]].x[j],j==K-1?‘\n‘:‘ ‘);
83         }
84     }
85     return 0;
86 }
时间: 2024-08-05 19:34:26

数据结构(KD树):HDU 4347 The Closest M Points的相关文章

bzoj 3053 HDU 4347 : The Closest M Points kd树

bzoj 3053 HDU 4347 : The Closest M Points  kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差较大的维度分开(建树常数大)或者每一位轮换分割(询问常数大),后者更快也更好些,以后就果断写第二种了. #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using

hdu 4347 The Closest M Points (kd树)

hdu 4347 题意: 求k维空间中离所给点最近的m个点,并按顺序输出  . 解法: kd树模板题 . 不懂kd树的可以先看看这个 . 不多说,上代码 . 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <cstring> 6 #include <queue> 7 #include <s

HDU 4347 - The Closest M Points - [KDTree模板题]

本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4347 Problem Description The course of Software Design and Development Practice is objectionable. ZLC is facing a serious problem

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+6,K = 5; #define squ(x) ((x)*(x)) int k,n,idx; struct Point { int x[K]; bool operator <(c

数据结构——k-d树

一直以为k-d树是一种高级的数据结构,和LCT可以并列:不过实际上没那么厉害. k-d树解决的是k维空间里的点的范围查找问题.k维空间必须满足两点之间的距离是欧几里德距离,比如二维的话,A(x1,y1)和B(x2,y2)的距离就是√(x1-x2)2+(y1-y2)2. k-d树是一颗二叉搜索树,只不过每个节点的键值不唯一了,都有k个键值.k-d树必须解决如何在这种情况下查找的问题.方法比较简单:对k-d树的每一层预先分配一下依赖哪个键值.比如2-d树维护平面直角坐标系的点,我们可以让第一层依赖于

【HDOJ】4347 The Closest M Points

居然是KD解. 1 /* 4347 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #i

K-D树问题 HDU 4347

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

HDU 4902 Nice boat(数据结构-线段树)

Nice boat Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and c

HDU 1394 Minimum Inversion Number (数据结构-线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9514    Accepted Submission(s): 5860 Problem Description The inversion number of a given number sequence a1, a2, ..., an