1057 Stack (30分)——甲级(栈、树状数组、二分)

7 篇文章 0 订阅
2 篇文章 0 订阅

Description:
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 (≤105​​ ). 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

题目大意:模拟一个特殊的栈,这个特殊的栈有一个特殊操作:查看当前栈中值为中间值的元素(不是中间位置)。

ps:如果每次PeekMedian都排序的话,显然太费时间了,会超时。

思路:

  • 根据题目对于中间的定义,在一个升序数组中,前缀出现次数 >= 数组大小+1的一半的第一个元素,即为中间值。 比如:1、17、17、18、90。
  • 前缀出现次数是指:前面所有数的出现次数之和。比如第一个17的前缀出现次数为:1+1 = 2。
  • 据此,我们可以将入栈元素作为数组下标(类似于桶排序),将其出现次数存入一个数组中,然后求前缀出现次数,满足要求的第一个数即为PeekMedian的输出。

考虑到简单的求前缀和会超时,我们使用树状数组(关于该数据结构以后再总结。。。)。而在寻找第一个满足的数的时候,利用二分

代码如下:

#include <iostream>
#include <stack>
#include <cstring>
#include <string>
#include <cstdio>
#define maxn 100001
#define lowbit(x) x&(-x)
using namespace std;
stack<int>s;
int c[maxn];
/*  树状数组基本操作的模板  */
void update(int i, int x)
{
    for(; i<=maxn; i+=lowbit(i))
        c[i] += x;
}
int getSum(int x)
{
    int sum = 0;
    for(; x; x -= lowbit(x))
        sum += c[x];
    return sum;
}

void PeekMedian(int half)
{
    int l = 1, r = maxn;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        if(getSum(mid) >= half)//getSum即求前缀出现次数。
            r = mid - 1;//如果相等的话,不一定是第一个出现的数,所以去掉右边区间,继续往左走。
        else
            l = mid + 1;
    }
    printf("%d\n", l);
}
int main()
{
    int n;
    scanf("%d", &n);
    while(n--)
    {
        char str[15];
        scanf("%s", str);
        if(str[1] == 'u')
        {
            int x;
            scanf("%d", &x);
            s.push(x);
            update(x, 1);//更新这个数的出现次数
        }
        else if(str[1] == 'o')
        {
            if(!s.empty())
            {
                int x = s.top();
                s.pop();
                update(x, -1);//弹出一次,次数-1
                printf("%d\n", x);
            }
            else
                printf("Invalid\n");
        }
        else
        {
            if(!s.empty())
            {
                PeekMedian((s.size()+1)/2);
            }
            else
                printf("Invalid\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值