#1057. Stack【栈 + 堆 + set】

该博客介绍了一种使用multiset数据结构实现一个支持Push、Pop和PeekMedian操作的栈的方法。在保持O(nlogn)时间复杂度的同时,可以方便地插入、删除元素并获取中位数。代码示例展示了如何处理各种操作,并确保数据结构的正确性。
摘要由CSDN通过智能技术生成

原题链接

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值