The Closest M Points
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

Can you help him solve this problem?
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.
#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的更多相关文章
- 【BZOJ 3053】The Closest M Points
KDTree模板,在m维空间中找最近的k个点,用的是欧几里德距离. 理解了好久,昨晚始终不明白那些“估价函数”,后来才知道分情况讨论,≤k还是=k,在当前这一维度距离过线还是不过线,过线则要继续搜索另 ...
- BZOJ 3053 The Closest M Points
[题目分析] 典型的KD-Tree例题,求k维空间中的最近点对,只需要在判断的过程中加上一个优先队列,就可以了. [代码] #include <cstdio> #include <c ...
- 【kd-tree】bzoj3053 The Closest M Points
同p2626.由于K比较小,所以不必用堆. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- 【BZOJ】3053: The Closest M Points(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
- 【HDOJ】4347 The Closest M Points
居然是KD解. /* 4347 */ #include <iostream> #include <sstream> #include <string> #inclu ...
- BZOJ3053: The Closest M Points
题解: 我们可以事先在堆里放入插入m个inf然后不断的比较当前值与堆首元素的大小,如果小于的话进入. 估计函数也可以随便写写... query的时候貌似不用保留dir... return 0写在 wh ...
- bzoj 3053 HDU 4347 : The Closest M Points kd树
bzoj 3053 HDU 4347 : The Closest M Points kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差 ...
- 数据结构(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 ...
- poj:4091:The Closest M Points
poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...
- HDU 4347 - The Closest M Points - [KDTree模板题]
本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...
随机推荐
- Java-Runoob-高级教程-实例-数组:06. Java 实例 – 数组获取最大和最小值
ylbtech-Java-Runoob-高级教程-实例-数组:06. Java 实例 – 数组获取最大和最小值 1.返回顶部 1. Java 实例 - 数组获取最大和最小值 Java 实例 以下实例 ...
- 杂项-Java:jar 包与 war 包介绍与区别
ylbtech-杂项-Java:jar 包与 war 包介绍与区别 1.返回顶部 1. 做Java开发,jar包和war包接触的挺多的,有必要对它们做一个深入的了解,特总结整理如下: 1.jar包的介 ...
- python appium增加方法
1.测试过程中发现python client没有拨打电话的方法,因此去添加该方法 1.1查看源码 appium-base-driver/blob/master/lib/protocol/routes. ...
- (jsp/html)网页上嵌入播放器(常用播放器代码整理) http://www.jb51.net/article/37267.htm
网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助 这个其实很简单,只要在HTML上添加以上代码就O ...
- EL中拼接字符串的方法
近期在项目中碰到一个需要在JSP页面中比较两String类型的值的问题,于是想当然的写了如下代码: <c:if test="${'p'+longValue=='p1'}"&g ...
- 部署mariadb数据库到linux(源码编译安装)
各种库: apt install -y build-essential zlib1g-dev libpcre3 libpcre3-dev unzip cmake libncurses5-dev lib ...
- Hadoop2.0的基本构成总览
Hadoop1.x和Hadoop2.0构成图对比 Hadoop1.x构成: HDFS.MapReduce(资源管理和任务调度):运行时环境为JobTracker和TaskTracker: Hadoop ...
- elastisSearch-aggregations
运行结果 统计每个学员的总成绩 这个是索引库使用通配符 优先在本地查询 只在本地节点中查询 只在指定id的节点里面进行查询 查询指定分片的数据 参考代码ESTestAggregation.java p ...
- 红帽yum源安装报错initscripts-9.49.41-1.el7.x86_64 conflicts redhat-release < 7.5-0.11" ?
https://access.redhat.com/solutions/3425111 环境 Red Hat Enterprise Linux 7 问题 yum fails to apply upda ...
- DataSanp App与Rest, WebBroker App的区别
DataSanp App与Rest, WebBroker App的区别 datasnap server :选择这一项,我们得到的将是一个独立EXE的三层服务器应用程序(TCP及HTTP两种模式) To ...