题解
2024-07-25 14:09:15
发布于:广东
16阅读
0回复
0点赞
题解
通过维护两个stack
, 一个用来存数据, 另一个是单调栈
即可不会Tle
代码
#include <iostream>
#include <stack>
using namespace std;
typedef stack<int> sint;
sint s;
sint s_max;
int op_count, op_code, temp;
int main() {
cin >> op_count;
while (op_count--) {
cin >> op_code;
if (op_code == 0) {
cin >> temp;
s.push(temp);
if (s_max.empty() || s_max.top() < temp)
s_max.push(temp);
else
s_max.push(s_max.top());
} else if (op_code == 1) {
if (!s.empty())
s.pop(), s_max.pop();
} else if (op_code == 2)
cout << (s_max.empty() ? 0 : s_max.top()) << '\n';
}
return 0;
}
这里空空如也
有帮助,赞一个