njupt 会漏的栈

出题原意是考双端队列,但是因为A题要让大家都过,所以把数据搞得很小。
第一份代码是纯数组模拟栈暴搞的,遇到大数据会完蛋,第二份是用双端队列模拟,可以胜任大数据的情况。

/*
    纯数组模拟栈暴搞
*/
#include <iostream>
using namespace std;
int S[10005];
int K, top , m, x, bottom;
int main()
{
    string s;
    cin >> K >> m;
    while(m--) {
        cin >> s;
        if(s == "push") {
            cin >> x;
            S[top++] = x;
            if(top - bottom > K) bottom++;
        } else {
            if(top == bottom) cout << "Empty!" << endl;
            else cout << S[--top] << endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

int max_size, m;
deque<int> dq;

int main() {
    scanf("%d %d", &max_size, &m);
    while(m--) {
        char op[16]; scanf("%s", op);
        if(op[1] == 'u') {
            int x; scanf("%d", &x);
            dq.push_back(x);
            if(dq.size() > max_size) dq.pop_front();
        } else if(op[1] == 'o') {
            if(dq.size() == 0) puts("Empty!");
            else /* */ printf("%d\n", dq.back()), dq.pop_back();
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值