Rails
题意:入栈顺序为1到n,问你出栈顺序是否满足要求,例如有5列车,出栈顺序54321可以,但54123不行。
用栈模拟一下即可
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n;
int a[N];
stack<int> s, t;
int main()
{
while(cin >> n){
if(n == 0){
break;
}
int m;
while(cin >> m){
if(m == 0){
break;
}
while(!s.empty())
s.pop();
a[1] = m;
for (int i = 2; i <= n; i++){
cin >> a[i];
}
int start = 1, end = 1;
int flag = 1;
while(end <= n){
if(start == a[end]){
start++;
end++;
}else if(!s.empty() && s.top() == a[end]){
s.pop();
end++;
}else if(start <= n){
s.push(start++);
}else{
flag = 0;
break;
}
}
printf("%s\n", flag ? "Yes" : "No");
}
cout << endl;
}
return 0;
}