博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ3053:The Closest M Points(K-D Teee)
阅读量:6958 次
发布时间:2019-06-27

本文共 3525 字,大约阅读时间需要 11 分钟。

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

Solution

还是K-D Tree模板,不过这个是真正的多维KDT,做的时候把原来的0/1扩展到多维就好了

查询m远的时候开个大根堆,当答案小于堆顶的时候就push进去,然后query内部稍微改一下

因为query的时候lans和rans忘了赋初值调了半天emmm……

Code

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #define N (50000+1000) 8 #define INF 1e16 9 using namespace std; 10 11 struct P 12 { 13 long long dis,num; 14 bool operator < (const P &a) const { return dis
q; 18 19 struct Node 20 { 21 long long Max[5],Min[5],d[5],lson,rson; 22 bool operator < (const Node &a) const { return d[D]
r) return 0; 51 long long mid=(l+r)>>1; 52 D=opt; nth_element(p+l,p+mid,p+r+1); 53 Tree[mid]=p[mid]; 54 Tree[mid].lson=Build((opt+1)%k,l,mid-1); 55 Tree[mid].rson=Build((opt+1)%k,mid+1,r); 56 Update(mid); return mid; 57 } 58 long long Get_min(long long now) 59 { 60 long long ans=0; 61 for (int i=0; i
Tree[now].Max[i]) ans+=sqr(T.d[i]-Tree[now].Max[i]); 64 if (T.d[i]
=1; --i)121 {122 for (int j=0; j

转载于:https://www.cnblogs.com/refun/p/9303084.html

你可能感兴趣的文章
云计算-从基础到应用架构系列索引
查看>>
[汇编] 统计字符
查看>>
Unity内置的shader include files
查看>>
Centos安装FTP服务器和配置
查看>>
Java 反射机制学习资料
查看>>
.NET 体系结构
查看>>
WIN7下回收站不小心删除的文件怎么恢复,免费数据恢复软件下载
查看>>
[Step By Step]SAP HANA PAL二次指数平滑算法实现DOUBLESMOOTH
查看>>
WPF 4 动态覆盖图标(Dynamic Overlay Icon)
查看>>
AngularJs $scope 里面的$apply 方法和$watch方法
查看>>
Back Track 5 之 Web踩点 && 网络漏洞
查看>>
Python初始化系统变量设置
查看>>
order by 多个条件
查看>>
SQL Server中In-Flight日志究竟是多少
查看>>
[ucgui] 彩色条函数
查看>>
链表中倒数第k个结点
查看>>
javaweb学习总结(三十六)——使用JDBC进行批处理
查看>>
spring cache
查看>>
c语言运算符优先级与while循环案例
查看>>
巧用对象的Length属性来裁剪对象内容
查看>>