HDU4347-The Closest M Points
来源:互联网 发布:攻心之知错不能改 编辑:程序博客网 时间:2024/05/20 00:52
The Closest M Points
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)Total Submission(s): 5507 Accepted Submission(s): 1743
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
题意:给你n个k维的点,有q次询问,每次给你一个点,求出离它最近的m个点
解题思路:kd-tree+优先队列
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cctype>#include <map>#include <cmath>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst LL INF = 0x3f3f3f3f3f3f3f3f;const int N = 50000 + 5;const int demension = 5;//二维struct node{int pos[demension];int ma[demension], mi[demension];int l, r;LL dis;bool operator<(const node &a)const{return dis < a.dis;}}a[N], x, pre, ans[15];priority_queue<node>q;int cmpDem;//以第cmpDem维作比较int root, n, qq, K, m, Size;bool cmp(const node &a, const node&b){return a.pos[cmpDem] < b.pos[cmpDem];}void Merge(int k){for (int i = 0; i < K; i++){if (a[k].l){a[k].ma[i] = max(a[k].ma[i], a[a[k].l].ma[i]);a[k].mi[i] = min(a[k].mi[i], a[a[k].l].mi[i]);}if (a[k].r){a[k].ma[i] = max(a[k].ma[i], a[a[k].r].ma[i]);a[k].mi[i] = min(a[k].mi[i], a[a[k].r].mi[i]);}}}LL dis(int k){LL dis = 0;for (int i = 0; i < K; i++){if (x.pos[i] < a[k].mi[i]) dis += 1LL * (a[k].mi[i] - x.pos[i])*(a[k].mi[i] - x.pos[i]);if (x.pos[i] > a[k].ma[i]) dis += 1LL * (x.pos[i] - a[k].ma[i])*(x.pos[i] - a[k].ma[i]);}return dis;}int build(int l, int r, int k){if (l > r) return 0;int mid = (l + r) / 2;//以第mid个元素为中心排序cmpDem = k;nth_element(a + l, a + mid, a + r + 1, cmp);//左右子树a[mid].l = build(l, mid - 1, (k + 1) % demension);a[mid].r = build(mid + 1, r, (k + 1) % demension);Merge(mid);return mid;}void query(int k){a[k].dis = 0;for (int i = 0; i < K; i++) a[k].dis += 1LL * (x.pos[i] - a[k].pos[i])*(x.pos[i] - a[k].pos[i]);if (Size < m) q.push(a[k]), Size++;else{pre = q.top();if (pre.dis > a[k].dis) q.pop(), q.push(a[k]);}LL dl = a[k].l ? dis(a[k].l) : INF, dr = a[k].r ? dis(a[k].r) : INF;if (min(dl, dr) >q.top().dis&&Size >= m) return;if (dl < dr){if (Size < m || dl < q.top().dis) query(a[k].l);if (Size < m || dr < q.top().dis) query(a[k].r);}else{if (Size < m || dr < q.top().dis) query(a[k].r);if (Size < m || dl < q.top().dis) query(a[k].l);}}int main(){while (~scanf("%d%d", &n, &K)){for (int i = 1; i <= n; i++){for (int j = 0; j < K; j++) scanf("%d", &a[i].pos[j]), a[i].mi[j] = a[i].ma[j] = a[i].pos[j];a[i].l = a[i].r = 0;}root = build(1, n, 0);scanf("%d", &qq);while (qq--){for (int i = 0; i < K; i++) scanf("%d", &x.pos[i]);scanf("%d", &m);Size = 0;query(root);printf("the closest %d points are:\n", m);for (int j = m; j >= 1; j--){ans[j] = q.top();q.pop();}for (int i = 1; i <= m; i++){for (int j = 0; j < K - 1; j++) printf("%d ", ans[i].pos[j]);printf("%d\n", ans[i].pos[K - 1]);}}}return 0;}
阅读全文
0 0
- HDU4347 The Closest M Points
- hdu4347-The Closest M Points
- HDU4347-The Closest M Points
- HDU4347--The Closest M Points(KD树)
- [KDTree] [优先队列] [HDU4347] The Closest M Points
- hdu4347 The Closest M Points(kdtree+stl)
- The Closest M Points
- [BZOJ3053]The Closest M Points
- poj:4091:The Closest M Points
- 【BZOJ 3053】 The Closest M Points
- HDU 4347 The Closest M Points
- 【35.43%】【hdu 4347】The Closest M Points
- BZOJ3053: The Closest M Points kdtree
- [BZOJ3053][KD树]The Closest M Points
- BZOJ 3053: The Closest M Points kdtree
- hdu 4347 The Closest M Points
- [bzoj3053][kd树]The Closest M Points
- hdu - 4347 - The Closest M Points - kd树
- IMU camera IMU 联合标定工具箱使用方法——Kalibr
- teachable-machine:探索机器学习如何工作,浏览器中实时浏览
- hihocoder 1580 Matrix dp+RMQ (带修改的最大子阵和)
- 文章标题
- gym 101201 fast-en J Shopping
- HDU4347-The Closest M Points
- GUI布局
- Android学习-常见的UI控件 RadioGroup和RadioButton
- 深度学习
- 我的六年CSDN博文写作感悟
- mysql简单查询
- 利用ajax请求的post如何传递数据
- 深入浅出的强化学习笔记(二)——使用OpenAI Gym实现游戏AI
- linux下如何搜索一个文件或程序所在位置