L2-032 彩虹瓶——栈

题目传送门:https://pintia.cn/problem-sets/994805046380707840/problems/1111914599412858889

题解:

//本题主要是栈的应用
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;

int main() {
    int N, M, K, temp;
    cin >> N >> M >> K;
    stack<int>s1;
    while (K--) {
        while (!s1.empty()) {
            s1.pop();
        }
        int f = 1;
        bool flag = false;
        for (int i = 0; i < N; i++) {
            cin >> temp;
            if (temp == f) {
                f++;
                while (!s1.empty()&& s1.top() == f) {
                    s1.pop();
                    f++;
                }
            }
            else {
                s1.push(temp);
                if (s1.size() > M) {
                    flag = true;
                }
            }
        }
        if (flag||!s1.empty()) {
            cout << "NO" << endl;
        }
        else {
            cout << "YES" << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Gzu_zb/p/10653912.html