出栈序列的合法性的解释 可以看这篇文章
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x;stack<int> a;
while (cin >> x && x != 0)
{
while (!a.empty())
{
a.pop(); //初始化
}
int c[1010] = {};
for (int i = 1; i <= x; i++)
{
int y;
cin >> y;
c[i] = y;
}
int t = 1; //输入元素的下标
int i;
for (i = 1; i <= x; i++)
{
if (i != c[t])
{
a.push(i);
}
if (i == c[t])
{
t++;
while (!a.empty()&&c[t]==a.top())
{
a.pop();
t++;
}
}
}
if (a.empty())
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}