题目1522:包含min函数的栈

时间:2021-07-06 07:25:53
#include <iostream>
#include <cstdio>
#include <stack> using namespace std; int main()
{
stack<int> st,st_min;
int n,x;
char s[2]; while(scanf("%d",&n)!=EOF)
{
for(int i=0; i<n; i++)
{
scanf("%s",&s);
if(s[0]=='s')
{
scanf("%d",&x);
st.push(x);
if(!st_min.empty())
{
int t=st_min.top();
if(x<t)
{
st_min.push(x);
}
}
else
{
st_min.push(x);
}
printf("%d\n",st_min.top());
}
else
{
if(!st.empty())
{
int t=st.top();
st.pop();
if(t==st_min.top())
{
st_min.pop();
}
}
if(!st_min.empty())
{
printf("%d\n",st_min.top());
}
else
{
printf("NULL\n");
} } }
}
return 0;
} /**************************************************************
Problem: 1522
User: jasonhaven
Language: C++
Result: Accepted
Time:20 ms
Memory:1524 kb
****************************************************************/