-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhitters.cpp
60 lines (52 loc) · 1.45 KB
/
hitters.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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main() {
int k;
cin >> k;
k--;
map<int, int> hitters;
map<int, int>::iterator it;
int x = 1;
while (x != -1) {
scanf("%d", &x);
if (x == 0) {
for (auto el : hitters) {
printf("%d ", el.first);
}
int value = hitters.begin()->first;
for(int i = k - (int)hitters.size(); i > 0; --i) {
printf("%d ", value);
}
printf("\n");
}
else {
if ((it = hitters.find(x)) != hitters.end()) {
it->second++;
}
else if (hitters.size() < k) {
hitters[x] = 1;
}
else {
bool saved = false;
int saved_key;
int saved_value;
for (it = hitters.begin(); it != hitters.end();) {
it->second--;
if (!saved && it->second >= 0) {
saved = true;
saved_key = it->first;
saved_value = it->second;
}
if (it->second <= 0)
it = hitters.erase(it);
else
it++;
}
if (hitters.empty())
hitters[saved_key] = saved_value;
}
}
}
}