Skip to content

Instantly share code, notes, and snippets.

@Cycatz
Created April 6, 2017 05:04
Show Gist options
  • Select an option

  • Save Cycatz/282964ab7fbe631df5a926c1ce76366a to your computer and use it in GitHub Desktop.

Select an option

Save Cycatz/282964ab7fbe631df5a926c1ce76366a to your computer and use it in GitHub Desktop.
[UVa]514 - Rails
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std ;
const int maxn = 10000 ;
int main(){
int n , k , num[maxn];
while(scanf("%d" , &n) == 1 && n){
num[0] = 0 ;
for(;;){
for(int i = 1 ; i <= n ; i++){
scanf("%d" , &num[i]);
if(!num[i])break ;
}
if(!num[1])break ;
int cur = n , out = n;
stack<int> sk ;
while(out >= 0){
if(num[out] == cur){out-- ; cur-- ;}
else if(!sk.empty() && sk.top() == cur){sk.pop() ; cur-- ;}
else sk.push(num[out--]) ;
}
if(sk.empty())puts("Yes");
else puts("No");
}
printf("\n") ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment