#UVa:514-Rails

灆洢 2012-07-22 20:07:58

利用要排成的組合狀況,試試看是否能用Stack辦到即可得解。

C++(0.080)

/*******************************************************/
/* UVa 514 Rails                                       */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2012/07/22                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;

int main(){
  stack<int> station;
  int N, p, now;
  bool exit, yon;
  while( scanf( "%d", &N ) != EOF && N != 0 ){
    exit = false;
    while( !exit ){
      now = 1;
      yon = true;
      for( int i = 0 ; i < N ; i++ ){
        scanf( "%d", &p );
        if( p == 0 ){
          exit = true;
          break;
        }
        if( yon ){
          if( now < p ){
            for( ; now < p ; now++ ) station.push(now);
              now++;
          }
          else if( now == p ) now++;
          else{
            if( p != station.top() ){
              yon = false;
            }
            else station.pop();
          }
        }
      }

      if( exit ) break;
      else if( yon ) printf( "Yes\n" );
      else printf( "No\n" );
    }
    printf( "\n" );
  }

  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料