-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1244.cpp
More file actions
66 lines (56 loc) · 1.32 KB
/
Copy path1244.cpp
File metadata and controls
66 lines (56 loc) · 1.32 KB
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
class Leaderboard {
public:
struct node
{
int p;
int s;
int v;
node(int p, int s, int v) : p(p), s(s), v(v){};
};
struct cmp{
bool operator()(node x, node y){return x.s < y.s;}
};
Leaderboard() {
}
void addScore(int playerId, int score) {
ms[playerId] += score;
node n(playerId, ms[playerId], ++mv[playerId]);
q.push(n);
}
//靠,你用堆把来算topk
//---结果空栈top,core了
int top(int K) {
int i = 0;
int sum = 0;
vector<node> v;
while(1)
{
node n = q.top();
q.pop();
if(n.v != mv[n.p])continue;
sum += n.s;
v.push_back(n);
if(++i == K)break;
}
for(int i = 0; i < v.size(); ++i)
{
q.push(v[i]);
}
return sum;
}
void reset(int playerId) {
node n(playerId, 0, ++mv[playerId]);
ms[playerId] = 0;
q.push(n);
}
priority_queue<node, vector<node>, cmp> q;
map<int, int> mv;
map<int, int> ms;
};
/**
* Your Leaderboard object will be instantiated and called as such:
* Leaderboard* obj = new Leaderboard();
* obj->addScore(playerId,score);
* int param_2 = obj->top(K);
* obj->reset(playerId);
*/