hdu 4347 The Closest M Points (kd-tree+优先队列,求M近邻)
题意:
给出若干个点,在给出一个定点,求距离该定点最近的m个点。
思路:
我们已经知道kd-tree可以得到最近邻,实际上M近邻,只需要维护一个size为M的优先队列就可以了。
需要注意,优先队列的元素一定要先定义小于关系orz
以及这次采用了轮盘转的策略划分维度,也就是按照深度,所有维度轮流作为split-method(实际用起来效果还是挺棒的orz
/* ***********************************************
Author :111qqz
Created Time :2017年10月08日 星期日 23时18分42秒
File Name :4347.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define PB push_back
14#define fst first
15#define sec second
16#define lson l,m,rt<<1
17#define rson m+1,r,rt<<1|1
18#define ms(a,x) memset(a,x,sizeof(a))
19typedef long long LL;
20#define pi pair < int ,int >
21#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=5E4+7;
7const int M = 10;
8int n,m,k,t;
9int idx;
10struct Point
11{
12 LL coor[M];
13 int id;
14 void print()
15 {
16 for ( int i = 1 ; i <= k ; i++)
17 printf("%lld%c",coor[i],i==k?'\n':' ');
18 }
19 bool operator < (const Point &u)const
20 {
21 return coor[idx]<u.coor[idx];
22 }
23}po[N];
24typedef pair< LL,Point >PI;
25priority_queue< PI >pq; //用优先队列一定要定义小于关系啊orz...我怎么这么傻
26struct KdTree
27{
28 Point p[N<<2];
29 bool leaf[N<<2];
30 void build ( int l,int r, int rt = 1,int dep=0)
31 {
32 if (l>r) return;
33 leaf[rt] = false;
34 leaf[rt<<1] = leaf[rt<<1|1] = true;
35 idx = dep % k;
36 int mid = (l+r)>>1;
37 nth_element(po+l,po+mid,po+r+1);
38 p[rt] = po[mid];
39 build(l,mid-1, rt<<1,dep+1);
40 build(mid+1,r,rt<<1|1,dep+1);
41 }
42 void query(Point tar,int rt=1,int dep=0)
43 {
44 if (leaf[rt]) return;
45 PI cur(0,p[rt]);
46 for ( int i = 1 ; i <= k ; i++) cur.fst += (p[rt].coor[i]-tar.coor[i])*(p[rt].coor[i]-tar.coor[i]);
47 int idx = dep%k;
48 int x=rt<<1,y=rt<<1|1,flag=0;
49 LL d = tar.coor[idx]-p[rt].coor[idx];
50 if (d>0) swap(x,y);
51 if (!leaf[x]) query(tar,x,dep+1);
52 if (pq.size()<m) pq.push(cur),flag=1;
53 else
54 {
55 if (cur.fst < pq.top().fst) pq.pop(),pq.push(cur);
56 if (d*d<pq.top().fst) flag = 1;
57 }
58 //cout<<pq.top().second.coor[0]<<" "<<pq.top().second.coor[1]<<endl;
59 if (!leaf[y]&&flag) query(tar,y,dep+1);
60 }
61}kd;
62int main()
63{
64 #ifndef ONLINE_JUDGE
65 freopen("./in.txt","r",stdin);
66 #endif
67 while (~scanf("%d %d",&n,&k))
68 {
69 for ( int i = 1 ; i <= n ; i++)
70 {
71 for ( int j = 1 ; j <= k ; j++) scanf("%lld",&po[i].coor[j]);
72 }
73 scanf("%d",&t);
74 kd.build(1,n);
75 while (t--)
76 {
77 Point ask;
78 for ( int i = 1 ; i <= k ; i++) scanf("%lld",&ask.coor[i]);
79 scanf("%d",&m);
80 kd.query(ask);
81 printf("the closest %d points are:\n",m);
82 Point ret[20];
83 //cout<<"pq_siz:"<<pq.size()<<" pq.top()"<<pq.top().sec.coor[1]<<endl;
84 for ( int i = 1 ; !pq.empty() ; i++) ret[i] = pq.top().second,pq.pop();
85 for ( int i = m ; i >= 1 ; i--) ret[i].print();
86 }
87 }
88 #ifndef ONLINE_JUDGE
89 fclose(stdin);
90 #endif
91 return 0;
92}