-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSequentially Ordinal Rank Tracker.cpp
42 lines (37 loc) · 1.2 KB
/
Sequentially Ordinal Rank Tracker.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
// Runtime: 1493 ms (Top 5.16%) | Memory: 148.6 MB (Top 58.09%)
struct compareMin
{
bool operator() (pair<int,string> p1, pair<int,string> p2) {
if(p1.first == p2.first) return p1.second < p2.second;
return p1.first > p2.first;
}
};
struct compareMax
{
bool operator() (pair<int,string> p1, pair<int,string> p2) {
if(p1.first == p2.first) return p1.second > p2.second;
return p1.first < p2.first;
}
};
class SORTracker {
public:
priority_queue <pair<int,string>, vector<pair<int,string>>, compareMin> min_heap;
priority_queue <pair<int,string>, vector<pair<int,string>>, compareMax> max_heap;
SORTracker() {}
void add(string name, int score) {
if(!min_heap.empty() && (min_heap.top().first < score || (min_heap.top().first == score && min_heap.top().second > name))) {
pair<int,string> t = min_heap.top();
min_heap.pop();
min_heap.push({score,name});
max_heap.push(t);
} else {
max_heap.push({score,name});
}
}
string get() {
pair<int,string> s = max_heap.top();
max_heap.pop();
min_heap.push(s);
return s.second;
}
};