summaryrefslogtreecommitdiff
path: root/514/rails.cpp
blob: d2f373b210f841c951578e2bc7a7403dd6ad2b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <stack>
using namespace std;

bool in(stack<int> s, int num){
    bool found=false;
    while(!found && s.size()>0){
	found=(s.top()==num);
	s.pop();
    }
    return found;
}

int main(){
    int num;
    cin>>num;
    while(num!=0){
	stack<int> s, orig;
	for(int i=num; i>0; i--)
	    orig.push(i);
	
	bool possible=true, end=false;

	while(!end){
	    int x;
	    cin>>x;
	    if(x==0){
		cout<<endl;
		end=true;
	    }
	    else{
		for(int i=0; i<num-1 && possible; i++){
		    while(!in(s, x)){
			s.push(orig.top());
			orig.pop();
		    }
		    if(x!=s.top())
			possible=false;
		    cin>>x;
		}
		possible? cout<<"Yes\n" : cout<<"No\n";
	    }
	}
	cin>>num;
    }
}