Maximum Element
You have an empty sequence, and you will be given queries. Each query is one of these three types:
1 x -Push the element x into the stack.
2 -Delete the element present at the top of the stack.
3 -Print the maximum element in the stack.
Input Format
The first line of input contains an integer, . The next lines each contain an above mentioned query. (It is guaranteed that each query is valid.)
Constraints
Output Format
For each type query, print the maximum element in the stack on a new line.
Sample Input
10
1 97
2
1 20
2
1 26
1 20
2
3
1 91
3
Sample Output
26
91
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main()
{
stack<int>v;
int n;
cin>>n;
while(n--)
{
int ch;
cin>>ch;
if(ch==1)
{
int x;
cin>>x;
v.push(max(x,v.size()>0?v.top():x));
cout<<v.top()<<endl;
cout<<max(x,v.size()>0?v.top():x)<<endl;
//stack容器的栈顶元素的读取函数为top 函数 将取出最后入栈的元素
//如果没有元素 输入元素
//如果有元素 比较输入元素与原来元素的大小 存大的
}
else if(ch==2)
{
v.pop();
}
else if(ch==3)
{
cout<<v.top()<<endl;
}
}
return 0;
}