在这道题中,题目说明了使用“先进后出”的工具,也就是使用栈。但是在这道题中会存在一些问题,因为在查询时只能使用栈顶元素,所以很不方便。可以采用存储最大值的方式。
具体形式看下图:
AC代码#include#include #include using namespace std; stack s; int main () { int n; cin >> n; int MAX = 0; for (int i = 1;i <= n;i++){ int x,y; cin >> x; if (x == 0){ cin >> y; if (y > MAX){ MAX = y; } s.push(MAX); } else if (x == 1){ if (!s.empty()){ s.pop(); if (!s.empty()){ MAX = s.top(); continue; } MAX = 0; } } else if (x == 2){ if (!s.empty()){ cout << s.top() << endl; } else { cout << 0 << endl; } } } }



