每日一题 特殊堆栈

7-5【进阶编程】【自选】【链栈或顺序栈自选】 特殊堆栈

分数 30

全屏浏览题目

切换布局

作者 陈越

单位 浙江大学

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

输入格式:

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

Push key
Pop
PeekMedian

其中 key 是不超过 105 的正整数;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

题目思路: 首先题目已经提示,要在代码当中融入栈(stack),push和pop分别代表放入元素,和出栈栈顶元素,但是题目当中还有另外一个要求:取中值(PeekMedian),要求把栈中的全部元素中的元素排序之后,输出他的中值。所以我们需要另开一个数组vector a,在push栈的同时,把元素导入数组当中,但是如果正常的导入,等要输出中值的时候在sort,如果元素很多的情况下,会导致时间超时,所以我们可以使用lower_bound来保证插入的过程当中元素的有序,即便插入边排序

a.insert(lower_bound(a.begin(), a.end(), x), x);//升序
//降序:upper_lower

然后再pop的时候要记得erase 

  a.erase(find(a.begin(),a.end(),x))

完整代码实现

#include<bits/stdc++.h>
using namespace std;
   vector<int>a;
int main()
{
    int n;
    cin>>n;
    string str;
    int x;
 
    stack<int> b;
    int count = 0;
    for(int i = 0;i<n;i++){
        cin>>str;
        if(str=="Pop") {
            if(count==0) cout<<"Invalid"<<endl;
            else {
                count--;
                int x=  b.top();
                cout<<b.top()<<endl;
                b.pop();
              //  dele(x);
                a.erase(find(a.begin(),a.end(),x));
                
            }
           
        }
         if(str=="Push"){
                cin>>x;
               b.push(x);
               a.insert(lower_bound(a.begin(), a.end(), x), x);
             //  b.push(x);
               count++;
          // sort(a.begin(),a.end());
           
            }
        if(str=="PeekMedian"){
             if(count==0) cout<<"Invalid"<<endl;
            else {
                int temp;
                if(count%2==0) temp = count/2;
                else temp = (count+1)/2;
                cout<<a[temp-1]<<endl;
            }
        }
    }
    return 0;
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的问。那么,pta7-5 特殊堆栈的 C 语言实现如下: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // 定义特殊堆栈结构体 struct SpecialStack { int data[MAX_SIZE]; // 堆栈数据 int min[MAX_SIZE]; // 堆栈中的最小值 int top; // 堆栈顶部指针 }; // 初始化特殊堆栈 void initStack(struct SpecialStack *stack) { stack->top = -1; } // 判断特殊堆栈是否为空 int isEmpty(struct SpecialStack *stack) { return stack->top == -1; } // 判断特殊堆栈是否已满 int isFull(struct SpecialStack *stack) { return stack->top == MAX_SIZE - 1; } // 向特殊堆栈中添加元素 void push(struct SpecialStack *stack, int item) { if (isFull(stack)) { printf("特殊堆栈已满,无法添加元素!\n"); return; } // 添加元素到堆栈中 stack->data[++stack->top] = item; // 更新最小值 if (stack->top == 0) { stack->min[stack->top] = item; } else { int min = stack->min[stack->top - 1]; stack->min[stack->top] = item < min ? item : min; } } // 从特殊堆栈中弹出元素 int pop(struct SpecialStack *stack) { if (isEmpty(stack)) { printf("特殊堆栈为空,无法弹出元素!\n"); return -1; } // 取出堆栈顶部元素 int item = stack->data[stack->top--]; // 更新最小值 stack->min[stack->top + 1] = 0; return item; } // 获取特殊堆栈中的最小值 int getMin(struct SpecialStack *stack) { if (isEmpty(stack)) { printf("特殊堆栈为空,无法获取最小值!\n"); return -1; } return stack->min[stack->top]; } // 测试函数 int main() { // 创建特殊堆栈 struct SpecialStack stack; initStack(&stack); // 向特殊堆栈中添加元素 push(&stack, 3); push(&stack, 5); push(&stack, 2); push(&stack, 1); // 获取特殊堆栈中的最小值 printf("特殊堆栈中的最小值为:%d\n", getMin(&stack)); // 弹出特殊堆栈中的元素 printf("特殊堆栈中的元素依次为:"); while (!isEmpty(&stack)) { printf("%d ", pop(&stack)); } printf("\n"); return 0; } ``` 以上是特殊堆栈的 C 语言实现代码,你可以根据需要进行修改、调试和运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值