1057. Stack (30)

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian – return the median value of all the elements in the stack. With N elements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((N+1)/2)-th if N is odd.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<= 105). Then N lines follow, each contains a command in one of the following 3 formats:

Push key
Pop
PeekMedian
where key is a positive integer no more than 105.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print “Invalid” instead.

Sample Input:
17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop
Sample Output:
Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid

题目地址:http://www.patest.cn/contests/pat-a-practise/1057

/* http://www.patest.cn/contests/pat-a-practise/1057 1057. Stack (30) // 可以插入相同的元素 multiset<int> qmax; // 从小到大排列 // 从大到小排列 cmp return a>b 表示大的在前面 multiset<int, cmp> qmin; // 或用模版比较greater<int> #include <functional> struct cmp // cmp本质上是一个比较的模版类 { bool operator()(int a, int b) { return a > b; } }; qmin.erase(qmin.find(t)); qmax.erase(qmax.begin()); // erase 所在的位置 qmin.insert(*qmax.begin()); *iterator 就是该值 */
#include <stdio.h>
#include <iostream> 
#include <vector> 
#include <map> 
#include <set> 
#include <string> 
#include <string.h>
#include <algorithm> 
#include <sstream>
#include <iterator>
#include <queue>
#include <stack>
#include <functional>
using namespace std;

#define N 100001

struct cmp
{
    bool operator()(int a, int b)
    {
        return a > b;
    }
};

int main()
{
    //freopen("in.txt", "r", stdin);
    int n, k;
    while (scanf("%d", &n) != EOF)
    {
        int i;
        char stmp[15];

        // 可以插入相同的元素
        multiset<int> qmax; // 从小到大排列
        // 从大到小排列 cmp return a>b 表示大的在前面 
        multiset<int, cmp> qmin; // greater<int> #include <functional>

        stack<int> staNum;
        int midval = -1;
        for (i = 0; i < n; i++)
        {
            scanf("%s", stmp);
            if (stmp[1] == 'o') // Pop
            {
                if (staNum.empty())
                {
                    printf("Invalid\n");
                }
                else{
                    int t = staNum.top();
                    staNum.pop();
                    printf("%d\n", t);
                    // 找到并删除
                    if (t > *qmin.begin())
                    {
                        qmax.erase(qmax.find(t));
                    }
                    else{
                        qmin.erase(qmin.find(t));
                    }

                    if(!staNum.empty())
                    {
                        int len1 = qmin.size();
                        int len2 = qmax.size();
                        if (len1 < len2)
                        {
                            qmin.insert(*qmax.begin());
                            qmax.erase(qmax.begin()); // erase 所在的位置
                        }
                        else if(len1 - len2 >= 2){
                            qmax.insert(*qmin.begin());
                            qmin.erase(qmin.begin()); // erase 所在的位置
                        }

                        midval = *qmin.begin();
                    }

                }
            }
            else if (stmp[1] == 'u') // Push
            {
                int tmp;
                scanf("%d", &tmp);
                staNum.push(tmp);
                if (tmp <= midval)
                {
                    qmin.insert(tmp);
                }
                else{
                    qmax.insert(tmp);
                }

                int len1 = qmin.size();
                int len2 = qmax.size();
                if (len1 < len2)
                {
                    qmin.insert(*qmax.begin());
                    qmax.erase(qmax.begin()); // erase 所在的位置
                }
                else if (len1 - len2 >= 2){
                    qmax.insert(*qmin.begin());
                    qmin.erase(qmin.begin()); // erase 所在的位置
                }

                midval = *qmin.begin();
            }
            else{ //if (stmp[1] == 'e'){ // PeekMedian
                if (staNum.empty())
                {
                    printf("Invalid\n");
                }
                else{
                    printf("%d\n", midval);
                }
            }
        }
    }
    //printf("\n");
    return 0;
}

你可能感兴趣的:(1057. Stack (30))