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

【BZOJ】3053: The Closest M Points(kdtree)的更多相关文章

  1. HDU 4347 The Closest M Points (kdTree)

    赤果果的kdTree. 学习传送门:http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html 其实就是二叉树的变形 #includ ...

  2. 【BZOJ】3668: [Noi2014]起床困难综合症(暴力)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3668 这题很简单.............. 枚举每一位然后累计即可.. QAQ,第一次以为能1A, ...

  3. 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...

  4. 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...

  5. 【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1601 很水的题,但是一开始我看成最短路了T_T 果断错. 我们想,要求连通,对,连通!连通的价值最小 ...

  6. 【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1600 说好的今天开始刷水.. 本题一开始我以为是排列组合,但是自己弱想不出来,只想到了如果四边有一条 ...

  7. 【BZOJ】1503: [NOI2004]郁闷的出纳员(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1503 这题没有看题解就1a了-好开心,, 其实后面去看题解发现他们的都很麻烦,其实有种很简单的做法: ...

  8. 【BZOJ】1269: [AHOI2006]文本编辑器editor(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1269 这题RE2次啊,好不爽啊,我一直以为是splay的问题,其实是数组开小了......(我老犯这 ...

  9. 【BZOJ】1975 [Sdoi2010]魔法猪学院(A*)

    题目 传送门:QWQ 分析 k短路,Astar.估价函数是终点向外跑的最短路. 显然不是正解qwq. 代码 // By noble_ // Astar algorithm // #include &l ...

随机推荐

  1. chm文件打开空白无内容的解决办法

    今天下载了个chm文件,但是打开空白,也没显示什么内容,经过一番研究之后终于可以正常显示了,下面把解决办法分享出来供大家参考下,谢谢.   工具/原料 windows7系统 chm文件 方法/步骤   ...

  2. poj 3020 最短路径覆盖 Antenna Placement

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7329   Accepted: 3635 ...

  3. tar: Removing leading `/’ from member names

    tar: Removing leading `/’ from member names+2 分类:Web服务器 标签:tar 3,910人浏览   这并不是一个错误,而是一个警告,原因很简单,就是你在 ...

  4. jquery博客收集的IE6中CSS常见BUG全集及解决方案

    今天的样式调的纠结,一会这边一会那么把jquery博客折腾的头大,浏览器兼容性.晚上闲着收集一些常见IE6中的BUG 3像素问题及解决办法 当使用float浮动容器后,在IE6下会产生3px的空隙,有 ...

  5. 《linux备份与恢复之一》.tar.bz2与.tar.gz格式的文本压缩率比较

    对于文本压缩,据说bzip的算法要优于gzip,从而拥有更好的压缩比.特地找了两个文件来做一下测试,以下为测试结果:   (1)源文件为591MB, .tar.bz2文件为61MB(10.32%), ...

  6. Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  7. 简单方便地扩充Python的系统路径

    参考: http://www.elias.cn/Python/PythonPath?from=Develop.PythonPath http://v2in.com/pth-file-usage-in- ...

  8. DP:Apple Catching(POJ 2385)

    牛如何吃苹果 问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的 ...

  9. codeforces B. Valera and Contest 解题报告

    题目链接:http://codeforces.com/problemset/problem/369/B 题目意思:给出6个整数, n, k, l, r, sall, sk ,需要找出一个满足下列条件的 ...

  10. javascript的onbeforeunload函数在IOS上运行

    今天在做项目的时候,组长让我用iPad测试一下前面写的离线缓存,后退不刷新页面,发现在iPad上onbeforeunload函数在iPad上一带而过,不运行??? 无奈之下,发现原来在IOS上,有自己 ...