-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1057-1.cpp
74 lines (66 loc) · 1.13 KB
/
A1057-1.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
74
/*
* author: zhouyuhao
* created: 2023-04-12 16:45:24
* modified: 2023-04-12 17:08:43
* item: Programming Ability Test
* site: Shahu
*/
#include <cmath>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 1e5 + 1;
const int sqr = sqrt(maxn);
stack<int> st;
vector<int> b(sqr), ht(maxn);
void emplace(int v) {
st.emplace(v);
++b[v / sqr];
++ht[v];
}
void pop() {
int v = st.top();
st.pop();
--b[v / sqr];
--ht[v];
cout << v << "\n";
}
void peekmedian(int k) {
int cnt = 0, bi = 0;
while (cnt + b[bi] < k) {
cnt += b[bi++];
}
int median = bi * sqr;
while (cnt + ht[median] < k) {
cnt += ht[median++];
}
cout << median << "\n";
}
int main(int argc, char const *argv[]) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
if (s.find("Push") != string::npos) {
int v;
cin >> v;
emplace(v);
} else if (s.find("Pop") != string::npos) {
if (st.empty()) {
cout << "Invalid\n";
} else {
pop();
}
} else {
if (st.empty()) {
cout << "Invalid\n";
} else {
int k = (st.size() + 1) / 2;
peekmedian(k);
}
}
}
return 0;
}