- #include<iostream>
- #include<cstdio>
- using namespace std;
- const int MA=1005;
- int stack[MA];
- int a[MA];
- int top=-1;
- int main(){
- int n;
- cin>>n;
- for(int i=1;i<=n;i++) scanf("%d",&a[i]);
- int cur=1;//cur 为进过栈的数字最大值 + 1, 初始 cur 为 1
- for(int i=1;i<=n;i++){
- while(stack[top]<a[i]){ // 进栈 cur 到 a[i]
- stack[++top]=cur++;
- }
- if(stack[top]==a[i]) top--;// 出栈 a[i]
- else{
- printf("NO\n");
- return 0;
- }
- }
- printf("YES\n");
- return 0;
- }
来源: http://www.bubuko.com/infodetail-3413088.html