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 .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\left( {{\bf{p}},{\bf{q}}} \right) = d\left( {{\bf{q}},{\bf{p}}} \right) = \sqrt {\sum\limits_{i = 1}^n {\left( {p_i - q_i } \right)^2 } }$

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

题意:

给出 $n$ 个 $K$ 维的点,又给出 $t$ 个查询, 每个查询也给出一个 $K$ 维的点,要求查询 $n$ 个点中距离这个点最近的 $M$ 个点。

题解:

KDTree模板题。


以下关于KDTree的小记:

1、简介 

KDTree(K-dimensional tree) 是一个支持多维空间的数据结构,主要是将空间内的点进行区域划分,快速维护有关空间点的操作,例如空间的最远(近)点对,区间搜索。

KDTree 的结构与线段树类似,只是线段树是对一维空间的操作,而 KDTree 是多维操作的,这也导致了 KDTree 的灵活性没有线段树高。

树上每个节点所维护的信息:

  1. 左右两个儿子
  2. 该点表示的空间范围(超长方体,当 $K=2$ 时为矩形,$K=3$ 时为长方体)
  3. 中位点(坐标等信息)

2.1、建树

假设我们已经有了一个描述 $K$ 维空间内的点的类Point,且我们已经有Point类型的 $a[0:n-1]$ 数组。

首先因为是空间划分,所以要循环递进地用垂直于各维度轴的超平面(或者线、平面)进行划分。

例如在二维平面上的划分,先用垂直 $x$ 轴的直线进行划分,再用垂直 $y$ 轴的直线进行划分,再用垂直 $x$ 轴的直线进行划分……

那么,假设现在要用垂直 $x$ 轴的直线划分在 $a[0:n-1]$ 数组内某个整数区间 $[l,r)$ 上的这些点,

首先初始化该点的空间范围,假设 $mid = \left\lfloor {\frac{{l + r}}{2}} \right\rfloor$,作为 $[l,r)$ 按 $x$ 坐标从小到大排序时的中位点位置,

然后用 nth_element(st,st+n,ed) 将 $[l,r)$ 分成了  $[l,mid)$ 和 $[mid+1,r)$ 两部分,当前节点存储 $a[mid]$ 这个点,

然后递归两个区间(作为左右儿子),当然,这两个区间则要用垂直 $y$ 轴的直线进行划分,以此类推。

(其中,调用 nth_element(st,st+n,ed) 方法可以求得 $[st,ed)$ 区间内所有元素中第 $n$ 小的元素,并把它放在第 $n$ 个位置上。注意,下标是从 $st+0$ 开始计数的,也就是说求第 $0$ 小的元素就是最小的数,同样地,第 $0$ 个位置是最前面的位置。)

2.2、查询 $k$ 近邻

$k$ 近邻是指找到第 $k$ 近的点,需要维护一个大顶堆,维护当前 $k$ 个点中的最远距离,如果当前点比最远距离要小,则更新大根堆,而且利用最远距离可以减掉那些不在当前第 $k$ 距离内的区间。 


AC代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn=5e4+10;
const int maxdim=7;

namespace KDTree
{
    int K;//维数
    inline double sqr(double x){return x*x;}
    struct Point
    {
        int x[maxdim];
        double distance(const Point &oth)const
        {
            double ret=0;
            for(int i=0;i<K;i++) ret+=sqr(x[i]-oth.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 cmpx
    {
        int div;
        cmpx(const int &_div){div=_div;}
        bool operator()(const Point &a,const Point &b)
        {
            for(int i=0;i<K;i++)
            {
                int k=(div+i)%K;
                if(a.x[k]!=b.x[k]) return a.x[k]<b.x[k];
            }
            return true;
        }
    };
    inline bool cmp(const Point &a,const Point &b,int div)
    {
        cmpx cp=cmpx(div);
        return cp(a,b);
    }

    struct Node //KDTree的节点
    {
        Point e;
        Node *lc,*rc;
        int div;
    }pool[maxn],*tail,*root;
    void init(){tail=pool;} //初始化KDTree
    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;
    }

    struct Qnode
    {
        Point p;
        double dist;
        Qnode(){}
        Qnode(Point _p,double _dist){p=_p; dist=_dist;}
        bool operator <(const Qnode &oth)const{return dist<oth.dist;}
    };
    priority_queue<Qnode> Q;
    void Search(const Point &p,Node *now,int div,int m) //在now节点子树下搜索p点的m近邻
    {
        if(now==NULL) return;
        if(cmp(p,now->e,div))
        {
            Search(p,now->lc,(div+1)%K,m);
            if(Q.size()<m)
            {
                Q.push(Qnode(now->e,p.distance(now->e)));
                Search(p,now->rc,(div+1)%K,m);
            }
            else
            {
                if(p.distance(now->e) < Q.top().dist)
                {
                    Q.pop();
                    Q.push(Qnode(now->e,p.distance(now->e)));
                }
                if(sqr((now->e.x[div])-(p.x[div])) < Q.top().dist) Search(p,now->rc,(div+1)%K,m);
            }
        }
        else
        {
            Search(p,now->rc,(div+1)%K,m);
            if(Q.size()<m)
            {
                Q.push(Qnode(now->e,p.distance(now->e)));
                Search(p,now->lc,(div+1)%K,m);
            }
            else
            {
                if(p.distance(now->e) < Q.top().dist)
                {
                    Q.pop();
                    Q.push(Qnode(now->e,p.distance(now->e)));
                }
                if(sqr((now->e.x[div])-(p.x[div])) < Q.top().dist) Search(p,now->lc,(div+1)%K,m);
            }
        }
    }

    void ClosestMPoints(const Point &p,int m) //搜索p点的m近邻
    {
        while(!Q.empty()) Q.pop();
        Search(p,root,0,m);
    }
};

int n,k;
int t,m;
KDTree::Point p[maxn];
vector<KDTree::Point> ans;

int main()
{
    while(cin>>n>>k)
    {
        KDTree::K=k;
        for(int i=0;i<n;i++) p[i].input();
        KDTree::init();
        KDTree::root=KDTree::Build(p,0,n,0);

        cin>>t;
        KDTree::Point o;
        for(int i=1;i<=t;i++)
        {
            o.input();
            scanf("%d",&m);
            KDTree::ClosestMPoints(o,m);
            printf("the closest %d points are:\n",m);
            ans.clear();
            while(!KDTree::Q.empty())
            {
                ans.push_back(KDTree::Q.top().p);
                KDTree::Q.pop();
            }
            for(int i=ans.size()-1;i>=0;i--) ans[i].output();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/dilthey/p/9763828.html