PAT 甲级 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​e5​​.

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
求栈中的第k大元素的时候,可以利用树状数组,在元素值的数值范围上建立树状数组

对于二分还有一个问题:
    假设某一时刻栈中元素为: 1 2 6 7 那么2、3、4、5进行get_sum时都是返回的2,但是3、4、5并不在栈中,因此返回3、4、5是不合理的,即需要返回大的是2
    二分有两个模板,可以处理目标有连续的多个相同值得情况,一个返回最左边的元素,一个返回的是最右边的元素,这里返回的是最左边的元素!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

int h[100010],head = 0,tail = -1;
int c[100010];

int lowbit(int x){
    return x & -x;
}

void update(int x,int d){
    for (int i = x; i < 100010 ; i += lowbit(i))
        c[i] += d;
}

int get_sum(int x){
    int ans = 0;
    for ( ; x ; x -= lowbit(x))
        ans = ans + c[x];
    return ans;
}

int main(){

    int n,key; string op;
    cin >> n;
    for (int i = 0; i < n; ++i){
        cin>>op;
        if (op == "Push"){
            cin>>key;
            update(key,1);
            h[++tail] = key;
        }else if (op == "PeekMedian"){
            if (head <= tail){
                int len = tail - head + 1;
                int k;
                if (len & 1) // 奇数
                    k = (len + 1) / 2;
                else   // 偶数
                    k = len / 2;
                int l = 1,r = 1e5;
                while(l < r){
                    int mid = l + r >> 1;
                    if (k <= get_sum(mid))
                        r = mid;
                    else l = mid + 1;
                }
                cout<< l <<endl;  // 此时l和r相同,因此 cout<<r<<endl; 也可以
            }else
                puts("Invalid");
        }else{
            if (head <= tail){
                update(h[tail],-1);
                printf("%d\n",h[tail]);
                tail--;
            }else{
                puts("Invalid");
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值