【题目描述】小 C 有一个队列他要对这个队列进行 q 次操作。操作共四种参数分别如下:1 x:这是第一种操作表示从队尾依次插入 123x;2 y:这是第二种操作表示弹出队头的前 y 个元素;3 z:这是第三种操作表示查询队列中的第 z 个元素;4:这是第四种操作表示查询队列中所有元素的最大值。你需要帮助他维护这个队列并对于每个第三种操作和第四种操作输出查询的答案。输入格式第一行两个整数 cq其中
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int main() {
    int c, q;
    cin >> c >> q;
    
    queue<int> que;
    for (int i = 0; i < q; i++) {
        int op;
        cin >> op;
        
        if (op == 1) {
            int x;
            cin >> x;
            for (int j = 1; j <= 3; j++) {
                que.push(j);
            }
            que.push(x);
        } else if (op == 2) {
            int y;
            cin >> y;
            for (int j = 0; j < y; j++) {
                que.pop();
            }
        } else if (op == 3) {
            int z;
            cin >> z;
            queue<int> temp = que;
            for (int j = 1; j < z; j++) {
                temp.pop();
            }
            cout << temp.front() << endl;
        } else if (op == 4) {
            queue<int> temp = que;
            int maxVal = temp.front();
            while (!temp.empty()) {
                maxVal = max(maxVal, temp.front());
                temp.pop();
            }
            cout << maxVal << endl;
        }
    }
    
    return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/iPSX 著作权归作者所有。请勿转载和采集!