bzoj 3053: The Closest M Points (KD-tree)

3053: The Closest M Points

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 863   Solved: 310
[ Submit][ Status][ Discuss]

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

[ Submit][ Status][ Discuss]

题解:KD-tree

对于多维KD-tree有两种构造方法,一种是最大方差法,另一种直接维度循环,貌似后者更快。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#define N 100003
#define M 7
#define inf 1000000000
using namespace std;
struct data{
	int d[M],mx[M],mn[M],dis;
	int l,r;
	bool operator <(const data &a) const
	{
		return dis<a.dis;
	}
}tr[N],tmp,ans[20];
int n,m,root,cmpd,a[M],k;
priority_queue<data> q;
void update(int now)
{
	int l=tr[now].l; int r=tr[now].r;
	for (int i=0;i<m;i++){
		if (l) tr[now].mx[i]=max(tr[now].mx[i],tr[l].mx[i]),
		       tr[now].mn[i]=min(tr[now].mn[i],tr[l].mn[i]);
		if (r) tr[now].mx[i]=max(tr[now].mx[i],tr[r].mx[i]),
		       tr[now].mn[i]=min(tr[now].mn[i],tr[r].mn[i]);
	}
}
int build(int l,int r,int now)
{
	now=now%m;
	for (int i=l;i<=r;i++) tr[i].dis=tr[i].d[now];
	int mid=(l+r)/2;
	nth_element(tr+l,tr+mid,tr+r+1);
	//for (int i=l;i<=r;i++,cout<<endl) 
	 //for (int j=0;j<m;j++) cout<<tr[i].d[j]<<" ";
	//cout<<endl; 
	for (int i=0;i<m;i++) 
	 tr[mid].mx[i]=tr[mid].mn[i]=tr[mid].d[i];
	if (l<mid) tr[mid].l=build(l,mid-1,now+1);
	if (r>mid) tr[mid].r=build(mid+1,r,now+1);
	update(mid);
	return mid;
}
int pow(int x)
{
	return x*x;
}
int dist(int now)
{
	int dis=0;
	for (int i=0;i<m;i++) {
		dis+=pow(max(0,a[i]-tr[now].mx[i]));
		dis+=pow(max(0,tr[now].mn[i]-a[i]));
	}
	return dis;
}
void query(int now)
{
	int d0=0,dl,dr;
	for(int i=0;i<m;i++)  d0+=(tr[now].d[i]-a[i])*(tr[now].d[i]-a[i]);
	tr[now].dis=d0;
	//cout<<q.size()<<endl;
	if (q.size()<k) q.push(tr[now]);
	else {
		data num=q.top();
		if (num.dis>d0) {
			q.pop();
			q.push(tr[now]);
		}
	}
	if (tr[now].l) dl=dist(tr[now].l);
	else dl=inf;
	if (tr[now].r) dr=dist(tr[now].r);
	else dr=inf;
	if (dl<dr) {
		if ((dl<q.top().dis||q.size()<k)&&dl!=inf) 
		  query(tr[now].l);
		if ((dr<q.top().dis||q.size()<k)&&dr!=inf) 
		  query(tr[now].r);
	}
	else {
		if ((dr<q.top().dis||q.size()<k)&&dr!=inf) 
		  query(tr[now].r);
		if ((dl<q.top().dis||q.size()<k)&&dl!=inf) 
		  query(tr[now].l);
	}
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	while (scanf("%d%d",&n,&m)!=EOF){
		memset(tr,0,sizeof(tr));
		for (int i=1;i<=n;i++) 
			for (int j=0;j<m;j++) scanf("%d",&tr[i].d[j]);
		root=build(1,n,0);
		int t; scanf("%d",&t);
		tmp.dis=inf;
		for (int i=1;i<=t;i++) {
			for (int j=0;j<m;j++) scanf("%d",&a[j]);
			scanf("%d",&k);
			while (!q.empty()) q.pop();
			query(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]);
			}
		}
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值