-
Notifications
You must be signed in to change notification settings - Fork 160
/
TwoDtopk.cpp
98 lines (75 loc) · 2.08 KB
/
TwoDtopk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Given n points on a two-dimensional plane, find the closest (euclidean distance) k points
to the point (0, 0).
*/
/*
solution1: use a maxheap. Build a maxheap with first k points, compare the rest
n-k points with the heap top, if larger than heap top, ignore, otherwise, add to heap.
If there are a lot of points stored on disk, we only need to scan the disk once.
O(nlogk) time, O(k) space
*/
/*
solution2: quickselect: O(n) average time, O(n^2) worst time
median of medians: O(n) worst time
If there are a lot of points stored on disk, we have to visit the disks many times.
*/
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct node {
double _x, _y;
node(double x, double y): _x(x), _y(y){}
node(): _x(0.0), _y(0.0){}
};
double Distance(node x, node target) {
return pow(x._x - target._x, 2) + pow(x._y - target._y,2);
}
node target(0,0);
struct GreaterNote {
bool operator() (const node a, const node b) {
return Distance(a, target) < Distance(b, target);
}
};
vector<node> GetKClosestNode(vector<node> nodes, int k) {
priority_queue<node, vector<node>, GreaterNote > q; //build the max heap
size_t size = nodes.size();
if (size <= k) return nodes;
for (size_t i = 0; i < k; i++ ){
q.push(nodes[i]);
}
size_t num = k;
while (num < size) {
node top = q.top();
if (Distance(nodes[num], target) < Distance(top, target)) {
q.pop();
q.push(nodes[num]);
}
}
vector<node> res;
while (!q.empty()){
res.push_back(q.top());
q.pop();
}
return res;
}
int main() {
vector<node> nodes;
node p1(0.2, 0.3);
node p2(0.1, 0.5);
node p3(0.4, 0.1);
node p4(0.5, 0.2);
node p5(0.0, 0.3);
nodes.push_back(p1);
nodes.push_back(p2);
nodes.push_back(p3);
nodes.push_back(p4);
nodes.push_back(p5);
int k = 3;
vector<node> res = GetKClosestNode(nodes,3);
for (size_t i = 0; i < res.size(); i++) {
cout<<"("<<res[i]._x<<","<<res[i]._y<<")";
}
return 0;
}