The Closest M Points

http://acm.hdu.edu.cn/showproblem.php?pid=4347

参考博客:https://blog.csdn.net/acdreamers/article/details/44664645#commentBox

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

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
 
Author
HIT
 
Source
 
模板题
 #include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define N 50005
using namespace std; int n,m,id;//n是点数,m是维度,id是当前切的维度 struct sair{
int p[];
bool operator<(const sair &b)const{
return p[id]<b.p[id];
}
}_data[N],data[N<<];
int flag[N<<]; priority_queue<pair<double,sair> >Q; void build(int l,int r,int rt,int dep){
if(l>r) return;
flag[rt]=;
flag[rt<<]=flag[rt<<|]=-;
id=dep%m;
int mid=l+r>>;
nth_element(_data+l,_data+mid,_data+r+);
data[rt]=_data[mid];
// if(l==r) return;
build(l,mid-,rt<<,dep+);
build(mid+,r,rt<<|,dep+);
} void query(sair p,int k,int rt,int dep){
if(flag[rt]==-) return;
pair<double,sair> cur(,data[rt]);//获得当前节点
for(int i=;i<m;i++){//计算当前节点到P点的距离
cur.first+=(cur.second.p[i]-p.p[i])*(cur.second.p[i]-p.p[i]);
}
int idx=dep%m;
int fg=;
int x=rt<<;
int y=rt<<|;
if(p.p[idx]>=data[rt].p[idx]) swap(x,y);
if(~flag[x]) query(p,k,x,dep+);
//开始回溯
if(Q.size()<k){
Q.push(cur);
fg=;
}
else{
if(cur.first<Q.top().first){
Q.pop();
Q.push(cur);
}
if((p.p[idx]-data[rt].p[idx])*(p.p[idx]-data[rt].p[idx])<Q.top().first){
fg=;
}
}
if(~flag[y]&&fg){
query(p,k,y,dep+);
}
} sair ans[]; int main(){
while(~scanf("%d %d",&n,&m)){
for(int i=;i<=n;i++){
for(int j=;j<m;j++){
scanf("%d",&_data[i].p[j]);
}
}
build(,n,,);
int t,k;
scanf("%d",&t);
while(t--){
sair tmp;
for(int i=;i<m;i++){
scanf("%d",&tmp.p[i]);
}
scanf("%d",&k);
while(!Q.empty()){
Q.pop();
}
printf("the closest %d points are:\n",k);
query(tmp,k,,);
for(int i=;i<k;i++){
ans[i]=Q.top().second;
Q.pop();
}
for(int i=k-;i>=;i--){
for(int j=;j<m;j++){
if(!j) printf("%d",ans[i].p[j]);
else printf(" %d",ans[i].p[j]);
}
printf("\n");
}
}
}
}

The Closest M Points的更多相关文章

  1. 【BZOJ 3053】The Closest M Points

    KDTree模板,在m维空间中找最近的k个点,用的是欧几里德距离. 理解了好久,昨晚始终不明白那些“估价函数”,后来才知道分情况讨论,≤k还是=k,在当前这一维度距离过线还是不过线,过线则要继续搜索另 ...

  2. BZOJ 3053 The Closest M Points

    [题目分析] 典型的KD-Tree例题,求k维空间中的最近点对,只需要在判断的过程中加上一个优先队列,就可以了. [代码] #include <cstdio> #include <c ...

  3. 【kd-tree】bzoj3053 The Closest M Points

    同p2626.由于K比较小,所以不必用堆. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  4. 【BZOJ】3053: The Closest M Points(kdtree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...

  5. 【HDOJ】4347 The Closest M Points

    居然是KD解. /* 4347 */ #include <iostream> #include <sstream> #include <string> #inclu ...

  6. BZOJ3053: The Closest M Points

    题解: 我们可以事先在堆里放入插入m个inf然后不断的比较当前值与堆首元素的大小,如果小于的话进入. 估计函数也可以随便写写... query的时候貌似不用保留dir... return 0写在 wh ...

  7. bzoj 3053 HDU 4347 : The Closest M Points kd树

    bzoj 3053 HDU 4347 : The Closest M Points  kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差 ...

  8. 数据结构(KD树):HDU 4347 The Closest M Points

    The Closest M Points Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Ot ...

  9. poj:4091:The Closest M Points

    poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...

  10. HDU 4347 - The Closest M Points - [KDTree模板题]

    本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...

随机推荐

  1. xcode编译失败

    iPhone自动化需要使用WDA.经过一段时间,再次编译,会出现WDA编译失败的问题,提示 The operation couldn't be completed. Unable to log in ...

  2. testNG断言

    https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThat 断言:Hamcrest - Matchers 对象: ...

  3. 关于Linux服务器磁盘空间占满问题的解决方法

    下面给大家分享一篇关于Linux服务器磁盘占满问题解决方法(/dev/sda3 满了),需要的的朋友参考下吧   下面我们一起来看一篇关于Linux服务器磁盘占满问题解决(/dev/sda3 满了), ...

  4. osx 安装redis

    brew install redis 想关文章 http://www.tuicool.com/articles/nM73Enr http://www.iteye.com/topic/1124400

  5. Microsoft Enterprise Library

    http://entlib.codeplex.com/ 微软企业库 现在已经到到6版本了 2013年更新的. https://www.microsoft.com/en-us/download/conf ...

  6. 对话框(VC_Win32)

    资源描述表中对话框定义 格式: 对话框名 DIALOG[载入特性] X,Y,Width,Height[设置选项] { 对话框控件定义; } 说明: 对话框名称: 标识对话框资源,可为一个字符串也可以为 ...

  7. php mysql_db_query()函数使用介绍

    php mysql_db_query()函数选择一个数据库并在其上执行查询,本文章向大家介绍mysql_db_query()函数的基本使用方法和实例,需要的朋友可以参考一下本文章. mysql_db_ ...

  8. IIS7根据PID查找对应的站点

    runas /user:administrator cmd cd \Windows\System32\inetsrv appcmd.exe list wp

  9. JavaScript中的坑

    内容:关于JavaScript中的一些蛋疼的问题以及面试笔试中常见的一些坑爹套路总结 此部分内容持续总结完善中... 1.undefined和null的区别 null: Null类型,代表空值,代表一 ...

  10. 3.2_k-近邻算法案例分析

        k-近邻算法案例分析 本案例使用最著名的”鸢尾“数据集,该数据集曾经被Fisher用在经典论文中,目前作为教科书般的数据样本预存在Scikit-learn的工具包中. 读入Iris数据集细节资 ...