题目1522:包含min函数的栈

来源:互联网 发布:河南省南乐县网络电视 编辑:程序博客网 时间:2024/06/04 18:44
#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****************************************************************/

0 0
原创粉丝点击