http://www.lydsy.com/JudgeOnline/problem.php?id=3053

本来是1a的QAQ。。。。

没看到有多组数据啊。。。。。斯巴达!!!!!!!!!!!!!!!!!

本题裸的kdtree,比上一题还要简单。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

对于当前点,判断进入左或右子树,然后看答案是否能过分割线。。如果能,进入右或左子树。。。。。。。。。并且如果答案个数小于k,也要进入。。

然后就浪吧。。。。。。。。。。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }const int N=100005, D=5;
struct node *null;
struct node {node *c[2];int p[D];void set(int _p[D]) { memcpy(p, _p, sizeof(p)); c[0]=c[1]=null; }static void init() { null=new node(); null->c[0]=null->c[1]=null; CC(null->p, 0); }
};
struct dat {node *ptr;ll dis;bool operator<(const dat &a) const { return dis<a.dis; }
};
priority_queue<dat> q;
struct kdtree {node T[N], *TI, *root;int now[D], di;node *newnode(int p[D]) { TI->set(p); return TI++; }kdtree() { di=0; TI=T; CC(now, 0); root=null; }static void init() { node::init(); }ll sqr(const ll &a) { return a*a; }ll dis(node *x, int p[D]) {ll ret=0;rep(i, di) ret+=sqr(x->p[i]-p[i]);return ret;}void insert(node *&x, int dep) {if(x==null) { x=newnode(now); return; }bool d=x->p[dep]<now[dep];insert(x->c[d], (dep+1)%di);}void ins(int p[D]) { memcpy(now, p, sizeof now); insert(root, 0); }void ask(node *x, const int &k, int dep) {if(x==null) return;static dat tp;tp.dis=dis(x, now); tp.ptr=x;q.push(tp); while((int)q.size()>k) q.pop();bool d=x->p[dep]<now[dep];ask(x->c[d], k, (dep+1)%di); if((int)q.size()<k || q.top().dis>sqr(now[dep]-x->p[dep])) ask(x->c[!d], k, (dep+1)%di);}void ask(int p[D], int k) {static node *dis[20];while(!q.empty()) q.pop();memcpy(now, p, sizeof now); ask(root, k, 0);printf("the closest %d points are:\n", k);int n=k;while((int)q.size()>k) q.pop();while(n) dis[--n]=q.top().ptr, q.pop();rep(j, k) {printf("%d", dis[j]->p[0]);for2(i, 1, di) printf(" %d", dis[j]->p[i]); puts(""); }}void clear() {root=null; TI=T; CC(now, 0); di=0;}
};
int p[D], di, n;
int main() {kdtree::init();kdtree a;while(~scanf("%d%d", &n, &di)) {a.clear();a.di=di;rep(i, n) { rep(k, di) read(p[k]); a.ins(p); }int t=getint();while(t--) {rep(k, di) read(p[k]);read(n);a.ask(p, n);}}return 0;
}

  


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

k-d tree

【BZOJ】3053: The Closest M Points(kdtree)相关推荐

  1. 【BZOJ】1034: [ZJOI2008]泡泡堂BNB(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1034 弱的比弱的强就用,强的比强的强就用: 否则弱的和强的比. 输的情况就是2n-ans(b,a), ...

  2. 【BZOJ】2021: [Usaco2010 Jan]Cheese Towers(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2021 噗,自己太弱想不到. 原来是2次背包. 由于只要有一个大于k的高度的,而且这个必须放在最顶,那 ...

  3. 【BZOJ】2019: [Usaco2009 Nov]找工作(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2019 spfa裸题.....将飞机场的费用变成负,然后spfa找正环就行了 #include < ...

  4. 【BZOJ3609】人人尽说江南好(博弈论)

    [BZOJ3609]人人尽说江南好(博弈论) 题面 BZOJ 洛谷 题解 昨天考试的时候,毒瘤出题人出了一个\(noip\)博弈十合一然后他就被阿鲁巴了,因为画面残忍,就不再展开. 这题是他的十合一中 ...

  5. 【linux】Valgrind工具集详解(八):Memcheck命令行参数详解

    [linux]Valgrind工具集详解(五):命令行详解中不够全,在此专门针对Memcheck工具中的命令行参数做一次详细的解释. Memcheck命令行选项 –leak-check=<no| ...

  6. 【Netty】入门Netty官方例子解析(二)Time Server

    本文承接上文<[Netty]入门Netty官方例子解析(一)写个 Discard Server> ,接下来讲解官网文档中Netty入门官方例子第二个例子 Time Server 原文这个章 ...

  7. 【 FPGA 】FIR 滤波器结构和优化(一)之滤波器的对称性(Filter Symmetry)

    这部分描述滤波器以及如何在FIR滤波器的IP核设计中优化它们的使用. 滤波器的对称性(Filter Symmetry) 很多滤波器的单位脉冲响应拥有明显的对称性,通常可以利用这种对称性来最小化算术要求 ...

  8. 【转】VS 安全开发生命周期(SDL)检查

    [转]VS 安全开发生命周期(SDL)检查 前面在学习使用google的protobuf时在VS2012中一直无法编译编译通过,经过查找一些资料原来发现,并不是protobuf的问题,而是自己在使用V ...

  9. ctab法提取dna流程图_【分子】DNA的提取与检测(下)——质粒DNA

    文章来自GongZH:[植为一生] [分子]DNA的提取与检测(下)--质粒DNA​mp.weixin.qq.com 爱米娜桑,大家好我们的分子生物学实验又回来聊.我们继续DNA的提取故事,就很舒服. ...

最新文章

  1. coreseek最大检索数只有1000的问题!
  2. Android如何使用so文件和Android studio中导入so
  3. 安装mysql8.019命令符_MySQL 8.0.19安装教程(windows 64位)
  4. 每个工程师都应该了解的:聊聊幂等
  5. WPF轮播图实现方式(二)
  6. TOP命令 详解CPU 查看多个核心的利用率按1
  7. 手机滤镜怎么移植到ps上_一分钟教你电脑端Lr、Ps滤镜预设/手机端Lr预设如何导入【图文教程】...
  8. scrapy猫眼爬虫
  9. 《IT项目管理》读书笔记(5) —— 项目时间管理
  10. 黑苹果系统是服务器系统,黑苹果各种版本的说明 _ 黑苹果乐园
  11. 灰色预测模型matlab预测20个数据,怎么matlab灰色模型预测这组数据的下一...
  12. 被退回的劳务派遣工需要支付补偿金吗?
  13. CDATA标签的用法
  14. 中年刘强东背后,是已婚男性的集体挣扎
  15. 2021高考成绩等位线查询四川,泸州医学院的麻醉专业收分请问下四川考生一 – 手机爱问...
  16. 天狮集团新零售有什么优势?天狮直销等于天狮新零售?
  17. XXL-JOB快速安装使用教程
  18. python获取mysql中的数据供js调用_python 读取mysql数据至csv文件中,并发送邮件
  19. The Street View House Numbers (SVHN) Dataset
  20. 平板打印机适合那些行业

热门文章

  1. 未来农业科技:物联网如何阻止全球粮荒
  2. 要如何努力,才干成为非常厉害的人?!
  3. linux下crontab实现定时服务详解
  4. 构建多域名Exchange 2010邮件系统
  5. acronym与abbr
  6. Share一个整理美化JavaScript代码的在线工具
  7. 无人驾驶油电混动牵引车_比纯电动更轻,省油率高达10%!解读首台国产非插电式混动卡车...
  8. Python 2 和 3 的区别及兼容技巧
  9. 使用MLX90640自制红外热像仪(四):stm32f103c8t6刷320x240分辨率屏的时间测试
  10. stm32通用定时器的PWM输出