1057 Stack (30 分)

本文介绍了一种在栈数据结构中实现额外的PeekMedian操作的方法,该操作返回栈中所有元素的中位数。通过使用树状数组和二分搜索,避免了直接排序的高时间复杂度,实现了高效查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 这里因为要寻找整个数组的中值,并且数组范围为1e5,所以这里使用sort感觉一定是会超时的,所以换用树状数组,和求逆序数的思路类似,这里寻找位置为前面有(_end+1)/2-1个数字的点,所以可以采用二分搜索的方法

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long ll;
const int MAXN = 101000;

char s[10];
int a[MAXN],num;
int tree[MAXN];

void Invalid(){
    printf("Invalid\n");
}

void add(int i,int x)
{
    while(i < MAXN){
        tree[i] += x;
        i += i&-i;
    }
}

int read(int pos)
{
    int sum = 0;
    while(pos > 0){
        sum += tree[pos];
        pos -= pos & -pos;
    }
    return sum;
}
int _Binary(int pos){
    int L = 0,R = MAXN;
    while(L <= R)
    {
        int mid = (L+R) >> 1;
        int num = read(mid);
        if(num > pos)
            R = mid - 1;
        else if(num <= pos)
            L = mid + 1;
    }
    return L;
}
int main()
{
    int _end = 0;
    int n;
    scanf("%d",&n);
    for(int i = 0;i < n;i ++)
    {
        scanf("%s",s);
        if(strcmp(s,"Pop") == 0)
        {
            if(_end == 0)
                Invalid();
            else
                printf("%d\n",a[--_end]),add(a[_end],-1);
        }
        else if(strcmp(s,"PeekMedian") == 0)
        {
            if(_end == 0)
                Invalid();
            else{
                printf("%d\n",_Binary((_end+1)/2-1));   //寻找节点前有(_end+1)/2-1 个数字比当前小的点
            }
        }
        else
        {
            scanf("%d",&num);
            a[_end++] = num;
            add(num,1);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值