L3-002 特殊堆栈 (30分) 【模拟 or 树状数组】

目录

一、首先想到的是用vector模拟,但是超时了

二、然后在网上学习了其他同学的做法:用stack模拟输入输出,用vector每次按顺序插入值,用lower_bound()函数

三、后来又看到柳婼大神的“树状数组求第k大的数”解法


L3-002 特殊堆栈 (30分)

堆栈是一种经典的后进先出的线性结构,相关的操作主要有“入栈”(在堆栈顶插入一个元素)和“出栈”(将栈顶元素返回并从堆栈中删除)。本题要求你实现另一个附加的操作:“取中值”——即返回所有堆栈中元素键值的中值。给定 N 个元素,如果 N 是偶数,则中值定义为第 N/2 小元;若是奇数,则为第 (N+1)/2 小元。

输入格式:

输入的第一行是正整数 N(≤10​5​​)。随后 N 行,每行给出一句指令,为以下 3 种之一:

Push key
Pop
PeekMedian

其中 key 是不超过 10​5​​ 的正整数;Push 表示“入栈”;Pop 表示“出栈”;PeekMedian 表示“取中值”。

输出格式:

对每个 Push 操作,将 key 插入堆栈,无需输出;对每个 Pop 或 PeekMedian 操作,在一行中输出相应的返回值。若操作非法,则对应输出 Invalid

输入样例:

17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop

输出样例:

Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid

一、首先想到的是用vector模拟,但是超时了

Pop就是删除第一个元素,Push就把元素插入到最前面,PeekMedian的话就先拷贝到另外一个vector,然后排序,输出中间值。

超时应该是排序那一步占用了太多时间。

超时代码:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v;
void push()
{
    int t;
    scanf("%d", &t);
    v.insert(v.begin(), t);
}
void pop()
{
    if (v.empty())
    {
        printf("Invalid\n");
        return;
    }
    printf("%d\n", v[0]);
    v.erase(v.begin());
}
void peekMedian()
{
    if (v.empty())
    {
        printf("Invalid\n");
        return;
    }
    vector<int> tmp = v;
    sort(tmp.begin(), tmp.end());
    int len = tmp.size();
    printf("%d\n", tmp[len % 2 == 0 ? len / 2 - 1 : len / 2]);
}
int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        getchar();
        char str[20];
        scanf("%s", str);
        if (str[1] == 'u')
            push();
        else if (str[1] == 'o')
            pop();
        else
            peekMedian();
    }
    //system("pause");
    return 0;
}

二、然后在网上学习了其他同学的做法:用stack模拟输入输出,用vector每次按顺序插入值,用lower_bound()函数

这样查中值的时候不用每次都进行排序了,可以节约大量时间。

 lower\_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper\_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。 

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
vector<int> v;
stack<int> s;
void push()
{
    int t;
    scanf("%d", &t);
    s.push(t);
    auto it = lower_bound(v.begin(), v.end(), t);
    v.insert(it, t);
}
void pop()
{
    if (s.empty())
    {
        printf("Invalid\n");
        return;
    }
    printf("%d\n", s.top());
    auto it = lower_bound(v.begin(), v.end(), s.top());
    v.erase(it);
    s.pop();
}
void peekMedian()
{
    if (s.empty())
    {
        printf("Invalid\n");
        return;
    }
    int len = s.size();
    printf("%d\n", v[len % 2 == 0 ? len / 2 - 1 : len / 2]);
}
int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        getchar();
        char str[20];
        scanf("%s", str);
        if (str[1] == 'u')
            push();
        else if (str[2] == 'o')
            pop();
        else
            peekMedian();
    }
    //system("pause");
    return 0;
}

三、后来又看到柳婼大神的树状数组求第k大的数”解法

树状数组储存每个数出现的次数,然后二分查询第k个数首次出现的地方。

#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int c[maxn];
stack<int> s;
int lowbit(int i)
{
    return i & (-i);
}
void update(int x, int v)
{
    for (int i = x; i < maxn; i += lowbit(i))
        c[i] += v;
}
int getsum(int x)
{
    int sum = 0;
    for (int i = x; i >= 1; i -= lowbit(i))
        sum += c[i];
    return sum;
}
void PeekMedian()
{
    int left = 1, right = maxn, k = (s.size() + 1) / 2;
    while (left < right)
    {
        int mid = (left + right) >> 1;
        //int pp = getsum(mid);
        if (getsum(mid) >= k)
            right = mid;
        else if (getsum(mid) < k)
            left = mid + 1;
    }
    printf("%d\n", left);
}
int main()
{
    int n;
    scanf("%d", &n);
    while (n--)
    {
        getchar();
        char str[20];
        scanf("%s", str);
        if (str[1] == 'u')
        {
            int t;
            scanf("%d", &t);
            update(t, 1);
            s.push(t);
        }
        else if (str[1] == 'o')
        {
            if (s.empty())
            {
                printf("Invalid\n");
                continue;
            }
            update(s.top(), -1);
            printf("%d\n", s.top());
            s.pop();
        }
        else
        {
            if (s.empty())
            {
                printf("Invalid\n");
                continue;
            }
            PeekMedian();
        }
    }
    //system("pause");
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值