hdu4347 The Closest M Points(kdtree+stl)

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

分析:
再来胡一个多维的kdtree
对于多维KD-tree有两种构造方法,一种是最大方差法,另一种直接维度循环,貌似后者更快

这道题求的是欧氏距离
build和update大同小异
比较新奇的地方就是我们需要维护一个优先队列
按照到询问点的距离从小到大存储

这就涉及到了stl中的priority_queue的用法:

优先队列是队列的一种,
不过它可以按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序
每次的push和pop操作,队列都会动态的调整,以达到我们预期的方式来存储
注意:优先队列默认的是数据大的优先级高
所以我们无论按照什么顺序push一堆数,最终在队列里总是top出最大的元素
如果我们在队列里的是一个结构体,并且想要按照某一元素排序
这是我们就需要在结构体里重载一下“<”
(只能重载 < )

bool operator <(const node &a)const{
     ...
}

tip

多组数据

dis函数的写法:
这里写图片描述
我这个zz老是忘了在dis函数最后返回值!!!

hdu不支持行末多余空格。。。

这里写代码片
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

const int N=100003;
const int INF=0x33333333;
struct node{
    int l,r,d[6],mx[6],mn[6],dis;
    bool operator <(const node &a)const
    {
        return dis<a.dis;
    }
};
node t[N],ans[20];
int n,k,T,m,root,a[6];
priority_queue<node> q;

void update(int bh)
{
    int lc=t[bh].l;
    int rc=t[bh].r;
    for (int i=0;i<m;i++)   //m:维度 
    {
        if (lc)
        {
            t[bh].mn[i]=min(t[bh].mn[i],t[lc].mn[i]);
            t[bh].mx[i]=max(t[bh].mx[i],t[lc].mx[i]);
        }
        if (rc)
        {
            t[bh].mn[i]=min(t[bh].mn[i],t[rc].mn[i]);
            t[bh].mx[i]=max(t[bh].mx[i],t[rc].mx[i]);
        }
    }
}

int build(int l,int r,int D)
{
    D%=m;
    int mid=(l+r)>>1;
    for (int i=l;i<=r;i++) t[i].dis=t[i].d[D];   //以这一维为基准 
    nth_element(t+1+l,t+1+mid,t+1+r);
    for (int i=0;i<m;i++) t[mid].mx[i]=t[mid].mn[i]=t[mid].d[i];
    if (l!=mid) t[mid].l=build(l,mid-1,D+1);
    if (r!=mid) t[mid].r=build(mid+1,r,D+1);
    update(mid);
    return mid; 
}

int dis(int now)
{
    int d=0;
    for (int i=0;i<m;i++)
    {
        if (a[i]<t[now].mn[i]) d+=(t[now].mn[i]-a[i])*(t[now].mn[i]-a[i]);
        if (a[i]>t[now].mx[i]) d+=(a[i]-t[now].mx[i])*(a[i]-t[now].mx[i]);
    }
    return d;   ///
}

void ask(int now)
{
    int d0=0,dl,dr;
    for (int i=0;i<m;i++) d0+=(t[now].d[i]-a[i])*(t[now].d[i]-a[i]);
    t[now].dis=d0;
    if (q.size()<k) q.push(t[now]);
    else
    {
        node r=q.top();
        if (r.dis>d0)   //找到前k小的 
        {
            q.pop();
            q.push(t[now]);
        }
    }
    if (t[now].l) dl=dis(t[now].l);
    else dl=INF;
    if (t[now].r) dr=dis(t[now].r);
    else dr=INF;
    if (dl<dr)
    {
        if ((dl<q.top().dis||q.size()<k)&&dl!=INF) ask(t[now].l);
        if ((dr<q.top().dis||q.size()<k)&&dr!=INF) ask(t[now].r);
    }
    else
    {
        if ((dr<q.top().dis||q.size()<k)&&dr!=INF) ask(t[now].r);
        if ((dl<q.top().dis||q.size()<k)&&dl!=INF) ask(t[now].l);
    }
}

int main()
{
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        memset(t,0,sizeof(t));
        for (int i=1;i<=n;i++)
            for (int j=0;j<m;j++) scanf("%d",&t[i].d[j]);
        root=build(1,n,0);
        scanf("%d",&T);
        while (T--)
        {
            for (int i=0;i<m;i++) scanf("%d",&a[i]);
            scanf("%d",&k);
            while (!q.empty()) q.pop();   ///
            ask(root);
            printf("the closest %d points are:\n",k);
            for (int j=k;j>=1;j--)
            {
                ans[j]=q.top();
                q.pop();
            }
            for (int j=1;j<=k;j++){
                for (int l=0;l<m-1;l++)
                    printf("%d ",ans[j].d[l]);
                printf("%d\n",ans[j].d[m-1]);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值