hdu 4347 The Closest M Points(kd树+优先队列)
来源:互联网 发布:java定义可变长度数组 编辑:程序博客网 时间:2024/05/22 04:36
The Closest M Points
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)Total Submission(s): 2338 Accepted Submission(s): 922
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?
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.
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.
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 21 11 33 422 322 31
Sample Output
the closest 2 points are:1 33 4the closest 1 points are:1 3
Author
HIT
Source
2012 Multi-University Training Contest 5
题目大意:求取一个点的m个最近的点
题目分析:kd树+优先队列 模板题
题目分析:kd树+优先队列 模板题
#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <cstdio>#define MAX (55555)#define K (5)#define INF (0x3f3f3f3f)#define pow(x) ((x)*(x))using namespace std;int k,n,idx;struct Point{ int x[K]; bool operator < ( const Point &u ) const { return x[idx] < u.x[idx]; }}po[MAX];typedef pair<double,Point> PDP;priority_queue<PDP> nq;struct Tree{ Point p[MAX<<2]; int son[MAX<<2]; void build ( int l , int r , int u = 1 , int dep = 0 ) { if ( l > r ) return; son[u] = r-l; son[u<<1] = son[u<<1|1] = -1; idx = dep%k; int mid = l + r >> 1; nth_element ( po+l , po+mid , po+r+1 ); p[u] = po[mid]; build ( l , mid-1 , u<<1 , dep+1 ); build ( mid+1 , r , u<<1|1 , dep+1 ); } void query ( Point a , int m , int u = 1 , int dep = 0 ) { if ( son[u] == -1 ) return; PDP nd ( 0 , p[u] ); for ( int i = 0 ; i < k ; i++ ) nd.first += pow ( nd.second.x[i] - a.x[i] ); int dim = dep%k , fg = 0; int x = u<<1 , y = u<<1|1; if ( a.x[dim] >= p[u].x[dim] ) swap ( x , y ); if ( ~son[x] ) query ( a , m , x , dep+1 ); if ( nq.size() < m ) nq.push ( nd ) , fg = 1; else { if ( nd.first < nq.top().first ) nq.pop() , nq.push ( nd ); if ( pow ( a.x[dim] - p[u].x[dim] ) < nq.top().first ) fg = 1; } if (~son[y]&&fg ) query ( a , m , y , dep+1 ); }}kd;void print ( Point &a ){ for ( int j = 0 ; j < k ; j++ ) printf ( "%d%c" , a.x[j] , j==k-1?'\n':' ' );}int main ( ){ while ( ~scanf ( "%d%d" , &n , &k ) ) { for ( int i = 0 ; i < n ; i++ ) for ( int j = 0 ; j < k ; j++ ) scanf ( "%d" , &po[i].x[j] ); kd.build ( 0 , n-1 ); int t,m; scanf ( "%d" , &t ); while ( t-- ) { Point ans; for ( int j = 0 ; j < k ; j++ ) scanf ( "%d" , &ans.x[j] ); scanf ( "%d" , &m ); kd.query ( ans , m ); printf ( "the closest %d points are:\n" , m ); Point pt[20]; for ( int j = 0 ; !nq.empty() ; j++ ) pt[j] = nq.top().second, nq.pop ( ); for ( int j = m-1 ; j >= 0 ; j-- ) print ( pt[j] ); } }}
0 0
- hdu 4347 The Closest M Points(kd树+优先队列)
- hdu - 4347 - The Closest M Points - kd树
- hdu 4347 The Closest M Points KD树
- HDU 4347 The Closest M Points KD-tree
- [BZOJ3053][KD树]The Closest M Points
- [bzoj3053][kd树]The Closest M Points
- HDU The Closest M Points(KD-tree模板题)
- HDU4347--The Closest M Points(KD树)
- [KDTree] [优先队列] [HDU4347] The Closest M Points
- HDU 4347 The Closest M Points
- 【35.43%】【hdu 4347】The Closest M Points
- hdu 4347 The Closest M Points
- [KD TREE] BZOJ 3053 The Closest M Points
- bzoj 3053: The Closest M Points (KD-tree)
- [BZOJ3053]The Closest M Points(kd-tree+堆)
- [BZOJ3053]The Closest M Points(KD-tree+堆)
- The Closest M Points
- HDU4347 The Closest M Points
- Expression Tree 上手指南 (三)
- jQuery用过的插件
- POJ 1228 Grandpa's Estate (稳定凸包)
- 10791 - Minimum Sum LCM
- C++二叉树线索化并遍历的示例代码
- hdu 4347 The Closest M Points(kd树+优先队列)
- sencha touch中点击overlay,会触发底下的输入框获得焦点的处理办法
- 关于错误提示Must explicitly describe intended ownership of an object array parameter
- C语言_分支结构
- acm2025
- iOS与WebService通信,内容包含“尖括号”返回异常
- 真实的面试题总结
- arm-linux 移植无线网卡RT3070驱动
- 在指定字符串中查找第一次找到所查找的字符和最后一次查找的到的字符