注意使用栈的时候的栈的调用过程,以及栈的函数等等。注释的代码是我乱搞的,也不知道对不对。
正确的姿势,是非注释部分模仿栈的调用过程写的代码。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
const int MAXN=1000+10;
int main()
{
int n;
while(cin>>n)
{
/*stack<int> s;
int a[100],b[100];
for(int i=1;i<=n;i++)
cin>>a[i];
int i=1;
s.push(a[i]);
i++;
int k=0;
while(i<=n)
{
int t=s.top();
while(a[i]<t&&i<=n)
{
s.push(a[i]);
i++;
}
int j=0;
// cout<<s.size()<<endl;
while(!s.empty())
{
j++;
b[k+j]=s.top();
s.pop();
}
k=k+j;
if(i<=n)
s.push(a[i]);
i++;
}
int j=1;
// cout<<s.size()<<endl;
while(!s.empty()) {b[k+j]=s.top();s.pop();j++;}
// for(int i=1;i<=n;i++)
// cout<<b[i]<<' ';
int x;
for(x=1;x<=n;x++)
if(x!=b[x])
break;
if(x!=n+1)
cout<<"No"<<endl;
else
cout<<"YES"<<endl;*/
int target[MAXN];
stack<int> s;
int A=1,B=1;
for(int i=1;i<=n;i++)
cin>>target[i];
int ok=1;
while(B<=n)
{
if(A==target[B]) {A++;B++;}
else if(!s.empty()&&s.top()==target[B]) {s.pop();B++;}
else if(A<=n) s.push(A++);
else {ok=0;break;}
}
cout<<(ok?"Yes":"No")<<endl;
}
return 0;
}