-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAILS.cpp
executable file
·103 lines (95 loc) · 1.6 KB
/
RAILS.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<bits/stdc++.h>
#define tam 1000
typedef struct {
int topo;
int item[tam];
} PILHA;
void initPilha(PILHA &p){
p.topo = -1;
}
bool pilhaVazia (PILHA p){
if (p.topo == -1){
return true;
}
else
return false;
}
bool pilhaCheia(PILHA p, int n) {
if (p.topo == n-1)
return true;
else
return false;
}
void push(PILHA &p, int x) {
p.item[++p.topo]=x;
}
int pop(PILHA &p) {
return (p.item[p.topo--]);
}
int top(PILHA p) {
if(pilhaVazia(p))
return 0;
else
return (p.item[p.topo]);
}
int main(){
PILHA p;
int n, n2 = 1, num=-1, pushed = 0, limite = 0;
initPilha(p);
scanf("%d", &n);
while(n!=0){
PILHA trem;
while (n2!=0 ){
initPilha(trem);
initPilha(p);
limite = 0;
num = -1;
push(p, 1);
pushed = 1;
for (int i =0; i<n; i++){
scanf("%d", &num);
if (num != 0){
push(trem, num);
if(limite==0){
for (int j = top(p); j<=n; j++){
if(!pilhaVazia (p) && !pilhaVazia(trem)){
if (top(trem) == top(p)){
pop(trem);
pop(p);
if (!pilhaVazia(p))
j = top(p);
else if (j<n && i<n-1 && pushed<n){
push(p, ++pushed);
break;
}
}
else if(pushed == n){
limite = 1;
break;
}
else if (!pilhaCheia(p, n) && j<n && pushed < n){
push(p, ++pushed);
}
}
}
}
}
else{
n2 = 0;
i = n+1;
}
}
if (n2!=0){
if (pilhaVazia(p) && pilhaVazia(trem))
printf("Yes\n");
else{
printf("No\n");
}
}
}
scanf("%d", &n);
n2=1;
printf("\n");
}
return 0;
}