车辆调度—模拟栈的操作

#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;
}

猜你喜欢

转载自www.cnblogs.com/w-w-t/p/12286955.html
今日推荐