HDU 4347 The Closest M Points (KD树)

HDU4347

KD树模板题,求K近邻,暴力,加个优先队列。

The Closest M Points

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

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

Author

HIT

Source

2012 Multi-University Training Contest 5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* ***************

Author :kuangbin

Created Time :2014/9/6 18:26:05

File Name :E:\2014ACM\专题学习\KD树\HDU4347.cpp

************************************************ */

#include <stdio.h>

#include <string.h>

#include <iostream>

#include <algorithm>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <string>

#include <math.h>

#include <stdlib.h>

#include <time.h>

using namespace std;

const int MAXN = 50010;

const int DIM = 10;

inline double sqr(double x){return x*x;}

namespace KDTree{

int K;//维数

struct Point{

int x[DIM];

double distance(const Point &b)const{

double ret = 0;

for(int i = 0;i < K;i++)

ret += sqr(x[i]-b.x[i]);

return ret;

}

void input(){

for(int i = 0;i < K;i++)scanf("%d",&x[i]);

}

void output(){

for(int i = 0;i < K;i++)

printf("%d%c",x[i],i < K-1?' ':'\n');

}

};

struct qnode{

Point p;

double dis;

qnode(){}

qnode(Point _p,double _dis){

p = _p; dis = _dis;

}

bool operator <(const qnode &b)const{

return dis < b.dis;

}

};

priority_queue<qnode>q;

struct cmpx{

int div;

cmpx(const int &_div){div = _div;}

bool operator()(const Point &a,const Point &b){

for(int i = 0;i < K;i++)

if(a.x[(div+i)%K] != b.x[(div+i)%K])

return a.x[(div+i)%K] < b.x[(div+i)%K];

return true;

}

};

bool cmp(const Point &a,const Point &b,int div){

cmpx cp = cmpx(div);

return cp(a,b);

}

struct Node{

Point e;

Node *lc,*rc;

int div;

}pool[MAXN],*tail,*root;

void init(){

tail = pool;

}

Node* build(Point *a,int l,int r,int div){

if(l >= r)return NULL;

Node *p = tail++;

p->div = div;

int mid = (l+r)/2;

nth_element(a+l,a+mid,a+r,cmpx(div));

p->e = a[mid];

p->lc = build(a,l,mid,(div+1)%K);

p->rc = build(a,mid+1,r,(div+1)%K);

return p;

}

void search(Point p,Node *x,int div,int m){

if(!x)return;

if(cmp(p,x->e,div)){

search(p,x->lc,(div+1)%K,m);

if(q.size() < m){

q.push(qnode(x->e,p.distance(x->e)));

search(p,x->rc,(div+1)%K,m);

}

else {

if(p.distance(x->e) < q.top().dis){

q.pop();

q.push(qnode(x->e,p.distance(x->e)));

}

if(sqr(x->e.x[div]-p.x[div]) < q.top().dis)

search(p,x->rc,(div+1)%K,m);

}

}

else {

search(p,x->rc,(div+1)%K,m);

if(q.size() < m){

q.push(qnode(x->e,p.distance(x->e)));

search(p,x->lc,(div+1)%K,m);

}

else {

if(p.distance(x->e) < q.top().dis){

q.pop();

q.push(qnode(x->e,p.distance(x->e)));

}

if(sqr(x->e.x[div]-p.x[div]) < q.top().dis)

search(p,x->lc,(div+1)%K,m);

}

}

}

void search(Point p,int m){

while(!q.empty())q.pop();

search(p,root,0,m);

}

};

KDTree::Point p[MAXN];

int main()

{

int n,k;

while(scanf("%d%d",&n,&k) == 2){

KDTree::K = k;

for(int i = 0;i < n;i++)p[i].input();

KDTree::init();

KDTree::root = KDTree::build(p,0,n,0);

int Q;

scanf("%d",&Q);

KDTree::Point o;

while(Q--){

o.input();

int m;

scanf("%d",&m);

KDTree::search(o,m);

printf("the closest %d points are:\n",m);

int cnt = 0;

while(!KDTree::q.empty()){

p[cnt++] = KDTree::q.top().p;

KDTree::q.pop();

}

for(int i = 0;i < m;i++)p[m-1-i].output();

}

}

return 0;

}
------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 380.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%