KD树--多维k近邻(HDU 4347: The Closest M Points)

 

The Closest M Points

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

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

 

 

前置:https://blog.csdn.net/Jaihk662/article/details/82531636

和HDU2966一样,唯一的区别就是前者只用求出最近的1个点

这道题要求最近的k个点,那就用个大顶堆维护一下当且最优的k个点就好了

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
int cur, deep, ans[50155];
priority_queue<pair<LL, int> > q;
typedef struct Point
{
	int id;
	LL x[7];
	bool operator < (const Point &b) const
	{
		if(x[cur]<b.x[cur])
			return 1;
		return 0;
	}
}Point;
Point temp[50122], p[50122], Aim;
void Create(int l, int r, int now)
{
	int m;
	if(l>=r)
		return;
	m = (l+r)/2;
	cur = now%deep;
	nth_element(p+l, p+m, p+r+1);
	Create(l, m-1, now+1);
	Create(m+1, r, now+1);
}
LL Dis(Point a, Point b)
{
	int i;
	LL ans = 0;
	for(i=0;i<=deep-1;i++)
		ans += (a.x[i]-b.x[i])*(a.x[i]-b.x[i]);
	return ans;
}
void Find(int l, int r, int now, int k)
{
	int m;
	LL judlen;
	pair<LL, int> v;
	if(l>r)
		return;
	m = (l+r)/2;
	v.first = Dis(Aim, p[m]), v.second = p[m].id;
	if(q.size()==k && v<q.top())
		q.pop();
	if(q.size()<k)
		q.push(v);
	cur = now%deep;
	judlen = p[m].x[cur]-Aim.x[cur];
	if(judlen>=0)
	{
		Find(l, m-1, now+1, k);
		if(judlen*judlen<q.top().first)
			Find(m+1, r, now+1, k);
	}
	else
	{
		Find(m+1, r, now+1, k);
		if(judlen*judlen<q.top().first)
			Find(l, m-1, now+1, k);
	}
}
int main(void)
{
	int n, i, j, k, Q, cnt;
	while(scanf("%d%d", &n, &deep)!=EOF)
	{
		for(i=1;i<=n;i++)
		{
			for(j=0;j<=deep-1;j++)
				scanf("%lld", &p[i].x[j]);
			p[i].id = i;
			temp[i] = p[i];
		}
		Create(1, n, 0);
		scanf("%d", &Q);
		while(Q--)
		{
			for(i=0;i<=deep-1;i++)
				scanf("%lld", &Aim.x[i]);
			scanf("%d", &k);
			for(i=1;i<=k;i++)
				q.push(make_pair((LL)1e18, -1));
			Find(1, n, 0, k);
			cnt = 0;
			while(q.empty()==0)
			{
				ans[++cnt] = q.top().second;
				q.pop();
			}
			printf("the closest %d points are:\n", k);
			for(i=cnt;i>=1;i--)
			{
				printf("%d", temp[ans[i]].x[0]);
				for(j=1;j<=deep-1;j++)
					printf(" %d", temp[ans[i]].x[j]);
				puts("");
			}
		}
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值