bzoj3053&hdu4347 The Closest M Points

http://www.elijahqi.win/2018/01/12/bzoj3053hdu4347-the-closest-m-points/
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(p,q)=D(q,p)=sqrt((q1-p1)^2+(q2-p2)^2+(q3-p3)^2…+(qn-pn)^2
Can you help him solve this problem?

软工学院的课程很讨厌!ZLC同志遇到了一个头疼的问题:在K维空间里面有许多的点,对于某些给定的点,ZLC需要找到和它最近的m个点。

(这里的距离指的是欧几里得距离:D(p, q) = D(q, p) = sqrt((q1 - p1) ^ 2 + (q2 - p2) ^ 2 + (q3 - p3) ^ 2 + … + (qn - pn) ^ 2)

ZLC要去打Dota,所以就麻烦你帮忙解决一下了……

【Input】

第一行,两个非负整数:点数n(1 <= n <= 50000),和维度数k(1 <= k <= 5)。
接下来的n行,每行k个整数,代表一个点的坐标。
接下来一个正整数:给定的询问数量t(1 <= t <= 10000)
下面2*t行:
  第一行,k个整数:给定点的坐标
  第二行:查询最近的m个点(1 <= m <= 10)

所有坐标的绝对值不超过10000。
有多组数据!

【Output】

对于每个询问,输出m+1行:
第一行:”the closest m points are:” m为查询中的m
接下来m行每行代表一个点,按照从近到远排序。

保证方案唯一,下面这种情况不会出现:
2 2
1 1
3 3
1
2 2
1
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
HINT

Source

k-d tree

蒟蒻我好菜啊qwq 每次都会把一个错误改掉同时新增另一个错误导致无法ac

这题题意:清晰明了 每次给出一个点 然后求和他相邻的最近的m个点都是谁

那么可以首先把所有点读进来 建一棵kd-tree 然后对于每次询问的点 我都取kd tree上暴力找

怎么暴力找 首先看看我这个点是否比我当前选择的一些可能值还要小 如果小就把队伍里最大的弹出 然后放入队伍 然后就和sjy摆棋类似了 大概就是我去算一下我左右区域 是否可能存在 答案如果存在就去暴力搞一搞 有个小的优化 就是我每次先push进优先队列m个inf 然后再算的时候直接看是否需要弹出再加入即可

#include<queue>
#include<cstdio>
#include<algorithm>
#define N 55000
#define inf 0x7f7fffff 
#define pa pair<int,int>
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int D,n,k,m,t,root,ans[15];
struct node1{
    int d[6];
    int& operator[](int x){return d[x];}
    friend bool operator<(node1 a,node1 b){return a[D]<b[D];}
}point[N],P;
struct node{
    int left,right,min[6],max[6];node1 x; 
}tree[N];
priority_queue<pa,vector<pa>,less<pa> >q;
inline void update(int x){
    int l=tree[x].left,r=tree[x].right;
    for (int i=0;i<k;++i) tree[x].min[i]=min(tree[x].min[i],min(tree[l].min[i],tree[r].min[i]));
    for (int i=0;i<k;++i) tree[x].max[i]=max(tree[x].max[i],max(tree[l].max[i],tree[r].max[i]));
}
inline void build(int &x,int l,int r,int dim){
    D=dim;int mid=l+r>>1;x=mid;nth_element(point+l,point+mid,point+r+1);tree[x].left=tree[x].right=0;
    tree[x].x=point[mid];for (int i=0;i<k;++i) tree[x].max[i]=tree[x].min[i]=point[mid][i];
    if (l<mid) build(tree[x].left,l,mid-1,(dim+1)%k);
    if (r>mid) build(tree[x].right,mid+1,r,(dim+1)%k);update(x);
}
inline int calc(node1 a,node1 b){
    int tmp=0;for (int i=0;i<k;++i) tmp+=(a[i]-b[i])*(a[i]-b[i]);return tmp;
}
inline int calc1(node a,node1 b){
    int tmp=0;
    for (int i=0;i<k;++i){
        tmp+=max(0,b[i]-a.max[i])*max(0,b[i]-a.max[i]);
        tmp+=max(0,a.min[i]-b[i])*max(0,a.min[i]-b[i]);
    }return tmp;
}
inline void query(int x){
    int tmp=calc(tree[x].x,P);
    if (q.top().first>tmp)q.pop(),q.push(make_pair(tmp,x));
    int disl=inf,disr=inf;
    if (tree[x].left) disl=calc1(tree[tree[x].left],P);if (tree[x].right) disr=calc1(tree[tree[x].right],P);
    if (disl<disr){
        if (q.top().first>disl) query(tree[x].left);
        if (q.top().first>disr) query(tree[x].right);
    }else{
        if (q.top().first>disr) query(tree[x].right);
        if (q.top().first>disl) query(tree[x].left);
    }
}
void print(int x){
    if (tree[x].left) print(tree[x].left);
    for (int i=0;i<k;++i) printf("%d ",tree[x].x[i]);printf("\n");
    for (int i=0;i<k;++i) printf("%d %d\n",tree[x].min[i],tree[x].max[i]);printf("dfsa\n");
    if (tree[x].right) print(tree[x].right);
}
int main(){
    freopen("bzoj3053.in","r",stdin);
    freopen("bzoj3053.out","w",stdout);
    while(~scanf("%d%d",&n,&k)){
        for (int i=0;i<k;++i) tree[0].min[i]=inf,tree[0].max[i]=-inf;
        for (int i=1;i<=n;++i)
            for (int j=0;j<k;++j) point[i][j]=read();
        build(root,1,n,0);t=read();//print(root);
        for (int i=1;i<=t;++i){
            for (int j=0;j<k;++j) P[j]=read();m=read();
            for (int j=0;j<m;++j) q.push(make_pair(inf,0));query(root);
            printf("the closest %d points are:\n",m);
            for (int j=m;j>=1;--j) ans[j]=q.top().second,q.pop();
            for (int j=1;j<=m;++j){
                for (int z=0;z<k-1;++z) printf("%d ",point[ans[j]][z]);printf("%d",point[ans[j]][k-1]);printf("\n");
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值