-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path895.cpp
73 lines (65 loc) · 1.58 KB
/
895.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
__________________________________________________________________________________________________
sample 220 ms submission
static const auto io_sync_off = []()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return nullptr;
}();
class FreqStack {
public:
unordered_map<int,int> freq;
unordered_map<int,stack<int>> m;
int maxfreq;
FreqStack()
{
freq.clear();
m.clear();
maxfreq=0;
}
void push(int x)
{
freq[x]++;
maxfreq=max(freq[x],maxfreq);
m[freq[x]].push(x);
}
int pop()
{
int ans=m[maxfreq].top();
m[maxfreq].pop();
freq[ans]--;
if(m[maxfreq].size()==0)maxfreq--;
return ans;
}
};
__________________________________________________________________________________________________
sample 67192 kb submission
class FreqStack {
public:
int idx;
FreqStack() {
idx = 0;
}
void push(int x) {
mp[x]++;
q.push({mp[x], {idx++, x}});
}
int pop() {
auto temp = q.top();
q.pop();
int val = temp.second.second;
if(--mp[val] == 0) mp.erase(val);
return val;
}
private:
priority_queue<pair<int, pair<int,int>>> q;
unordered_map<int,int> mp;
};
/**
* Your FreqStack object will be instantiated and called as such:
* FreqStack obj = new FreqStack();
* obj.push(x);
* int param_2 = obj.pop();
*/
__________________________________________________________________________________________________