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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章