#1057. Stack【栈 + 堆 + set】

原题链接

Problem Description:

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 N N elements, the median value is defined to be the ( N / 2 ) (N/2) (N/2)-th smallest element if N N N is even, or ( ( N + 1 ) / 2 ) ((N+1)/2) ((N+1)/2)-th if N N N is odd.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N ( ≤ 1 0 5 N (\leq 10^5 N(105). Then N N 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 1 0 5 10^5 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

Problem Analysis:

思路和 AcWing 106. 动态中位数 基本一致,只不过本题需要多维护一个删除操作,由于 priority_queue 不支持删除除堆顶外的元素的操作,我们可以把用到的对顶堆 priority_queue 换成 multiset,这样不仅时间复杂度依旧是 O ( n log ⁡ n ) O(n\log n) O(nlogn),还可以任意删除元素。具体细节见代码。

Code

#include <iostream>
#include <algorithm>
#include <set>
#include <cstring>
#include <stack>

using namespace std;

stack<int> stk;
multiset<int> up, down;

void adjust()
{
    
    
    while (up.size() > down.size())
    {
    
    
        down.insert(*up.begin());
        up.erase(up.begin());
    }
    while (down.size() > up.size() + 1)
    {
    
    
        auto it = down.end();
        it -- ;
        up.insert(*it);
        down.erase(it);
    }
}

int main()
{
    
    
    int n;
    scanf("%d", &n);
    char op[20];
    while (n -- )
    {
    
    
        scanf("%s", op);
        if (strcmp(op, "Push") == 0) 
        {
    
    
            int x;
            scanf("%d", &x);
            stk.push(x);
            // 上面不是空的的话,下面也一定不是空的,因为下面个数一定大于等于下面个数
            if (up.empty() || x < *up.begin()) down.insert(x);  
            else up.insert(x);
            // 调整上下个数关系
            adjust();
        }
        else if (strcmp(op, "Pop") == 0)
        {
    
    
            if (stk.empty()) puts("Invalid");
            else
            {
    
    
                int x = stk.top();
                stk.pop();
                printf("%d\n", x);
                auto it = down.end();
                it -- ;
                // multiset 中不能直接erase,否则会把所有的这个数全部删掉
                // 先find,再erase,就只会删除这一个数
                if (x <= *it) down.erase(down.find(x));  // erase
                else up.erase(up.find(x));
                
                adjust();
            }
        }
        else
        {
    
    
            if (stk.empty()) puts("Invalid");
            else
            {
    
    
                auto it = down.end();
                it -- ;
                printf("%d\n", *it);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/geraltofrivia123/article/details/121236674