代码如下
#include<iostream>
using namespace std;
const int N = 2000006;
int s[N];
int n;
int top=0;
int maxi=0;
void stack_in(int x) {
s[++top] = x;
if (s[top] > s[maxi])maxi = top;
}
void stack_out() {
if (top == 0)return;
if (maxi == top) {
maxi= 0;
for (int i = 1; i < top; i++) {
if (s[i] > s[maxi])maxi = i;
}
}
s[top--] = 0;
}
int stack_query(){
if (top == 0)return 0;
return s[maxi];
}
int main() {
cin >> n;
while (n--) {
int q;
cin >> q;
if (q == 0) {
int x;
cin >> x;
stack_in(x);
}
if (q == 1)stack_out();
if (q == 2)cout << stack_query()<<endl;
}
return 0;
}