1357:车厢调度 (栈)

【题目链接】

    http://ybt.ssoier.cn:8088/problem_show.php?pid=1357

【算法】

    栈模拟调度操作,一个指针指向入栈队列,如果出栈的编号大于指针指向的则指针后移并且车厢入栈,若小于且在栈头则出栈,反之序列不可行。

【代码】

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n,i,puz;
 4 int a[1010],b[1010],tot,p=1;
 5 int main()
 6 {
 7     scanf("%d",&n);
 8     for(i=1;i<=n;i++) a[i]=i;
 9     for(i=1;i<=n;i++) {
10         scanf("%d",&puz);
11         if(puz>=a[p]&&p<=n) {
12             while(puz>=a[p]&&p<=n) b[++tot]=a[p++];
13             tot--;
14         }else {
15             if(b[tot]==puz) tot--;
16             else {
17                 puts("NO");
18                 return 0;
19             }
20         }
21     } 
22     puts("YES");
23     return 0;
24 }
原文地址:https://www.cnblogs.com/Willendless/p/9403330.html