小红的数据结构

方法:栈,队列,模拟


#include<bits/stdc++.h>
using namespace std;
int a[100100];
int main() {
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<pair<int, int>> os(q);
    for (int i = 0; i < q; ++i) {
        int op;
        cin >> op;
        if (op == 1) {
            int x;
            cin >> x;
            os[i] = {1, x};
        } else {
            os[i] = {2, -1};
        }
    }
    stack<int> s;
    bool st = true;
    int ptr_stack = 0;
    queue<int> queue_sim;
    bool qu = true;
    int ptr_queue = 0;
    for (auto op : os) {
        if (op.first == 1) {
            s.push(op.second);
            queue_sim.push(op.second);
        } else {
            if (st && !s.empty()) {
                if (s.top() != a[ptr_stack]) {
                    st = false;
                } else {
                    s.pop();
                    ptr_stack++;
                }
            }
            if (qu && !queue_sim.empty()) {
                if (queue_sim.front() != a[ptr_queue]) {
                    qu = false;
                } else {
                    queue_sim.pop();
                    ptr_queue++;
                }
            }
        }
    }
    if (st && qu) {
        cout << "both";
    } else if (st) {
        cout << "stack";
    } else if (qu) {
        cout << "queue";
    } else {
        cout << "-1";
    }

    return 0;
}