-
Notifications
You must be signed in to change notification settings - Fork 2
/
A1057.cpp
65 lines (60 loc) · 1017 Bytes
/
A1057.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
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int MAXN = 100000 + 10;
const int sqrN = 316;
int N;
stack<int> st;
int block[sqrN] = {0}, table[MAXN] ={0};
void peekMidian(int K){
int sum = 0, idx = 0;
while(sum + block[idx] < K){
sum += block[idx++];
}
int num = sqrN * idx;
while(sum + table[num] < K){
sum += table[num++];
}
printf("%d\n", num);
}
void push(int x){
st.push(x);
++block[x/sqrN];
++table[x];
}
void pop(){
int x = st.top();
st.pop();
--block[x/sqrN];
--table[x];
printf("%d\n", x);
}
int main(){
char str[20];
int key;
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%s", str);
if(str[1] == 'u'){
scanf("%d", &key);
push(key);
}else if(str[1] == 'o'){
if(st.empty()){
printf("Invalid\n");
}else{
pop();
}
}else if(str[1] == 'e'){
if(st.empty()){
printf("Invalid\n");
}else{
int K = st.size();
if(K%2 == 0) K = K / 2;
else K = (K+1) /2;
peekMidian(K);
}
}
}
return 0;
}