火车按照编号从小到大进站,然后给出火车出站的次序,判断是否存在这种可能。
需要注意的是,并不是等到所有火车都进站了,火车才开始出站。火车的出入站遵从先进后出的规律,当前时间最后进的火车可以选择出站与否,这明显符合栈的特点。然后只要
注意别越界操作就行了。
Run Time: 0.03sec
Run Memory: 312KB
Code length: 886Bytes
Submit Time: 2011-12-07 13:54:52
// Problem#: 1509
// Submission#: 1044126
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int main()
{
int N;
int i, j;
int order[ 1000 ];
while ( scanf( "%d", &N ) ) {
if ( N == 0 )
break;
while ( scanf( "%d", &order[ 0 ] ) ) {
if ( order[ 0 ] == 0 )
break;
for ( i = 1; i < N; i++ )
scanf( "%d", &order[ i ] );
j = 0;
stack<int> station;
for ( i = 1; i <= N; i++ ) {
station.push( i );
while ( j < N && !station.empty() && order[ j ] == station.top() ) {
j++;
station.pop();
}
}
j == N ? cout << "Yes\n": cout << "No\n";
}
cout << endl;
}
return 0;
}