[jobdu]包含min函数的栈

老题,两个stack。其中一个维护min值就行了。

#include <iostream>
#include <stack>
using namespace std;
 
int main()
{
    int n;
    while (cin >> n)
    {
        stack<int> st;
        stack<int> min;
        while (n--)
        {
            char ch;
            cin >> ch;
            if (ch == 's')
            {
                int k;
                cin >> k;
                st.push(k);
                if (min.empty() || k < min.top())
                    min.push(k);
                cout << min.top() << endl;
            }
            else // assume ch == 'o'
            {
                int k = st.top();
                st.pop();
                if (min.top() == k)
                    min.pop();
                if (min.empty()) cout << "NULL" << endl;
                else cout << min.top() << endl;
            }
        }
    }

    return 0;
}

  

posted @ 2013-11-02 22:54  阿牧遥  阅读(198)  评论(0编辑  收藏  举报