1057 Stack (30 分) 简单思路(非树状数组法)

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 (≤10^5). 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 10^5.
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

问题链接:
https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592
解法:
题目意思是设计一个栈,但是这个栈可以查询栈内元素排序后的中位数 – PeekMedian 。使用直接模拟的方法是不可行的,数据量之大,不出意外的话肯定会意外的超时。
本题有许多解法,其中最流行的是使用树状数组来解,由于甲级的题纲规定树状数组是顶级考试内容,所以我使用了另一种简单思路来解。使用multi_set,每次push把key加入到set里面,pop时候找到key再删掉即可。还有就是求中位数,就是需要构造一个迭代器,一直指向中位数的位置即可,剩下的就是去维护这个迭代器了。
当加入元素时候,判断比中位数大还是小,然后决定插入后的迭代器++或者–。删除时候也是需要判断一下中位数的最后位置应该在哪里。还有就是删除的数等于中位数的时候,应该直接++到要删除的元素,因为可能删除的就是迭代器指向的内容。

AC代码:

#include<iostream>
#include<vector>
#include<set>
using namespace std;
int main()
{
    vector<int>stack;
    multiset<int>sorted;
    multiset<int>::iterator midVal;
    int N, key;
    string operation;
    cin >> N;
    while(N--)
    {
        cin >> operation;
        if(operation == "Pop"){
            if(stack.empty()) cout << "Invalid" << endl;
            else{
                int erase_num = stack.back();
                cout << erase_num << endl;
                stack.pop_back();
                if(erase_num == *midVal){//这个没办法 因为有可能删除midVal指向的元素,所以 只能慢慢找到他 属于投机取巧了
                    sorted.erase(sorted.find(erase_num));
                    midVal = sorted.begin();
                    for(int i=(sorted.size()-1)/2; i>0; --i) midVal++;
                }
                else if(erase_num > *midVal){
                    sorted.erase(sorted.find(erase_num));
                   if(!sorted.empty() && int(sorted.size()) % 2 == 0) midVal --;
                }
                else{
                    sorted.erase(sorted.find(erase_num));
                    if(!sorted.empty() && int(sorted.size()) % 2 == 1) midVal ++;
                }
            }
        }
        else if(operation == "PeekMedian"){
            if(stack.empty()) cout << "Invalid" << endl;
            else{
                cout << *midVal << endl;
            }
        }
        else if(operation == "Push"){
            cin >> key;
            stack.push_back(key);
            sorted.insert(key);
            if(sorted.size() == 1) midVal = sorted.begin();
            else{
                if(key >= *midVal && int(sorted.size()) % 2 == 1) midVal ++;
                else if(key < *midVal && int(sorted.size()) % 2 == 0) midVal --;
            }
        }
    }
    return 0;
}

在这里插入图片描述
当然推荐使用树状数组解决本题,因为树状数组很好用呀。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值