已知从1~n的数字序列,按顺序出栈。每个数字入栈后有两种选择:1-立即出栈,2-等待后面的数字入栈出栈后,该数字再出栈。
现在给出一个数字序列,求该数字序列是否合法?
#include<iostream>
#include<algorithm>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
class MinStack {
public:
stack<int> a;
stack<int> Min;
MinStack() {
}
void push(int n) {
if (a.size() == 0) {
a.push(n);
Min.push(n);
}
else {
a.push(n);
if (n < Min.top()) {
Min.push(n);
}
else {
Min.push(Min.top());
}
}
}
int pop