PAT甲级1057stack---对顶堆

前言

这题我搜了一下,好像没有人用对顶堆做,都是树状数组。事实上我第一次接触动态中位数就是用对顶堆做的,我就不服,我就要楞头用对顶堆做,但是这题我忙活了半天踩了好多坑。。。终于被我用堆顶堆搞出来的。

题目

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

我们来说说堆顶堆做法,左手放一个大根堆,右手放小根堆,然后我们维护一下两个堆的平衡,右手放较大值,左手放较小值。然后我们的小根堆的堆顶就是我们的中位数了。但是这题中位数做了严格定义,偶数时取较小的,我们要做个改变,然后就是涉及pop操作,所以我们用set进行模拟堆,然后记住!!!!!
pop操作后也要维护堆的平衡,好,我们上代码。

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

#define pii pair<int,int>

using namespace std;

const int N=100010;

int k,tt=-1;
set<pii> big,small;
pii stk[N];

int main(){
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    scanf("%d",&k);
    while(k--)
    {
        char str[15];
        scanf("%s",str);
        if(!strcmp(str,"Pop"))
        {
            if(tt==-1) puts("Invalid");
            else
            {
                auto temp=stk[tt--];
                small.erase(temp);
                big.erase(temp);
                if(small.size()>big.size()+1)
                {
                    auto it=small.begin();
                    big.insert(*it);
                    small.erase(it);
                }
                else if(big.size()>small.size())
                {
                    auto it=big.end();
                    it--;
                    small.insert(*it);
                    big.erase(it);

                }
                printf("%d\n",temp.first);
            }
            
        }
        else if(!strcmp(str,"Push"))
        {
            int x;
            scanf("%d",&x);
            stk[++tt]={x,k};
            if(small.empty())
                small.insert({x,k});
            else
            {
                auto t=small.begin()->first;
                if(x>t) small.insert({x,k});
                else big.insert({x,k});

                if(small.size()>big.size()+1)
                {
                    auto it=small.begin();
                    big.insert(*it);
                    small.erase(it);
                }
                else if(big.size()>small.size())
                {
                    auto it=big.end();
                    it--;
                    small.insert(*it);
                    big.erase(it);

                }
            }
            
        }
        else
        {
            if(small.size())
            {
                if((small.size()+big.size())%2!=0)
                    printf("%d\n",small.begin()->first);
                else
                    printf("%d\n",big.rbegin()->first);
                
            }
            else
            {
                puts("Invalid");
            }
            
        }
        
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值