-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Queue.cpp
More file actions
82 lines (74 loc) · 1.77 KB
/
Minimum_Queue.cpp
File metadata and controls
82 lines (74 loc) · 1.77 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;
// Minimum Queue, O(1)
template <typename T, typename Compare = less<T>>
struct MQueue {
stack<pair<T, T>> st1, st2;
Compare cmp = Compare();
void push(const T& x) {
T mn = st1.empty() ? x : min(x, st1.top().second, cmp);
st1.emplace(x, mn);
}
void pop() {
if (st2.empty()) {
while (!st1.empty()) {
T x = st1.top().first;
T mn = st2.empty() ? x : min(x, st2.top().second, cmp);
st1.pop();
st2.emplace(x, mn);
}
}
st2.pop();
}
T front() {
if (st2.empty()) {
while (!st1.empty()) {
T x = st1.top().first;
T mn = st2.empty() ? x : min(x, st2.top().second, cmp);
st1.pop();
st2.emplace(x, mn);
}
}
return st2.top().first;
}
T get() const {
if (st1.empty() || st2.empty()) {
return st1.empty() ? st2.top().second : st1.top().second;
}
return min(st1.top().second, st2.top().second, cmp);
}
int size() const {
return st1.size() + st2.size();
}
bool empty() const {
return st1.empty() && st2.empty();
}
};
void solve() {
int q;
cin >> q;
MQueue<int> mq;
while (q--) {
int op;
cin >> op;
if (op == 1) {
int x;
cin >> x;
mq.push(x);
} else if (op == 2) {
mq.pop();
} else if (op == 3) {
cout << mq.get() << '\n';
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}