数据结构(KD树):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): 3285 Accepted Submission(s): 1201
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?
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.
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.
1 1
1 3
3 4
2
2 3
2
2 3
1
1 3
3 4
the closest 1 points are:
1 3
- #include <algorithm>
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <queue>
- using namespace std;
- const int maxn=;
- int cmpNo,K;
- struct Node{
- int x[],l,r,id;
- bool operator <(const Node &b)const{
- return x[cmpNo]<b.x[cmpNo];
- }
- };
- long long Dis(const Node &a,const Node &b){
- long long ret=;
- for(int i=;i<K;i++)
- ret+=(a.x[i]-b.x[i])*(a.x[i]-b.x[i]);
- return ret;
- }
- Node p[maxn];
- int Build(int l,int r,int d){
- if(l>r)return ;
- cmpNo=d;
- int mid=l+r>>;
- nth_element(p+l,p+mid,p+r+);
- p[mid].l=Build(l,mid-,(d+)%K);
- p[mid].r=Build(mid+,r,(d+)%K);
- return mid;
- }
- priority_queue<pair<long long,int> >q;
- void Kth(int l,int r,Node tar,int k,int d){
- if(l>r)return;
- int mid=l+r>>;
- pair<long long,int>v=make_pair(Dis(p[mid],tar),p[mid].id);
- if(q.size()==k&&v<q.top())q.pop();
- if(q.size()<k)q.push(v);
- long long t=tar.x[d]-p[mid].x[d];
- if(t<=){
- Kth(l,mid-,tar,k,(d+)%K);
- if(q.top().first>t*t)
- Kth(mid+,r,tar,k,(d+)%K);
- }
- else{
- Kth(mid+,r,tar,k,(d+)%K);
- if(q.top().first>t*t)
- Kth(l,mid-,tar,k,(d+)%K);
- }
- }
- int k,ans[];
- Node a[maxn];
- int main(){
- int n;
- while(scanf("%d%d",&n,&K)!=EOF){
- for(int id=;id<=n;id++){
- for(int i=;i<K;i++)
- scanf("%d",&p[id].x[i]);
- p[id].id=id;
- a[id]=p[id];
- }
- Build(,n,);
- int Q,tot;
- scanf("%d",&Q);
- Node tar;
- while(Q--){
- for(int i=;i<K;i++)
- scanf("%d",&tar.x[i]);
- scanf("%d",&k);
- printf("the closest %d points are:\n",k);
- for(int i=;i<=k;i++)q.push(make_pair(1e18,-));
- Kth(,n,tar,k,);tot=;
- while(!q.empty()){
- int id=(q.top()).second;q.pop();
- ans[tot++]=id;
- }
- for(int i=tot-;i>=;i--)
- for(int j=;j<K;j++)
- printf("%d%c",a[ans[i]].x[j],j==K-?'\n':' ');
- }
- }
- return ;
- }
数据结构(KD树):HDU 4347 The Closest M Points的更多相关文章
- bzoj 3053 HDU 4347 : The Closest M Points kd树
bzoj 3053 HDU 4347 : The Closest M Points kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差 ...
- hdu 4347 The Closest M Points (kd树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4347 题意: 求k维空间中离所给点最近的m个点,并按顺序输出 . 解法: kd树模板题 . 不懂kd树的可以先看看这个 . 不多说, ...
- hdu 4347 The Closest M Points(KD树)
Problem - 4347 一道KNN的题.直接用kd树加上一个暴力更新就撸过去了.写的时候有一个错误就是搜索一边子树的时候返回有当前层数会被改变了,然后就直接判断搜索另一边子树,搞到wa了半天. ...
- HDU 4347 - The Closest M Points - [KDTree模板题]
本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...
- HDU 4347 The Closest M Points (kdTree)
赤果果的kdTree. 学习传送门:http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html 其实就是二叉树的变形 #includ ...
- 【HDOJ】4347 The Closest M Points
居然是KD解. /* 4347 */ #include <iostream> #include <sstream> #include <string> #inclu ...
- hud 4347 The Closest M Points(KD-Tree)
传送门 解题思路 \(KD-Tree\)模板题,\(KD-Tree\)解决的是多维问题,它是一个可以储存\(K\)维数据的二叉树,每一层都被一维所分割.它的插入删除复杂度为\(log^2 n\),它查 ...
- KD树的极简单笔记(待后续更新)
今天(18.5.4)室友A突然问我算法怎么入门,兴奋之下给他安利了邓公的<数据结构>,然而他接着又问我能不能两周内快速入门,毕竟打算搞Machine Learning,然后掏出手机看了下他 ...
- K-D树问题 HDU 4347
K-D树可以看看这个博客写的真心不错!这里存个版 http://blog.csdn.net/zhjchengfeng5/article/details/7855241 HDU 4349 #includ ...
随机推荐
- RHEL7安装配置TigerVNC
TigerVNC使用非加密的链接,默认会被firewalld blocked 掉,想要 vnc正常工作就需要让firewalld开放相应的端口才行. vnc默认的端口号为5900,而每个vnc win ...
- Bash算术计算
1:$(( )) 2:$[ ] 3:`expr $x + $y` 4:bc命令 #!/bin/bash x= y= echo $(( x + y )) echo $[ $x + $y ] echo ` ...
- Oracle 卸载 不干净
关闭oracle相关的服务 注册表删除(可能因为oracle及windows的版本不同注册表信息也有些差异): 开始è输入regedit 打开注册表编辑器删除下面的目录 HKEY_LOCAL_MACH ...
- C#语法糖之开篇
本人虽然大学不是学的计算机但是对于IT行业的热爱,依然决然进军IT行业了,自从踏进这个行业到现在也已经3年多了,从去年开发通过网上 了解博客园后深深的爱上这儿了,这里有很多牛人,通过拜读他们的代码,让 ...
- oracle数据库读取操作系统的物理文件-转载,待完善
--源地址不详 --创建目录SQL> create directory dir_xls as '/home/oracle'; Directory created. --给用户授权SQL> ...
- JavaScript - 测试 jQuery
测试 JavaScript 框架库 - jQuery 引用 jQuery 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 <script> 标签,其 s ...
- ASP.NET网站实现中英文转换(本地化资源)
主要内容: 1. 简单例子 2. 进一步认识Localization 3. 语言转换 4. 解决方案 一. 简单例子 下面通过一个简单的例子来说明利用Localization来实现本地化是那么的简单, ...
- 主成份分析PCA
Data Mining 主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有6 ...
- 一些javascript免费中文书籍
在这里谢谢那些无私的人~~这些内容来自这里:我只把js的链接粘到这里了~ 还有其它技术文档, 实在是太多了, 多的看都看不完!!! Google JavaScript 代码风格指南 Google JS ...
- 《作业控制系列》-“linux命令五分钟系列”之十
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...