-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path514.go
57 lines (52 loc) · 949 Bytes
/
514.go
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
// UVa 514 - Rails
package main
import (
"fmt"
"os"
)
func solve(coaches []int) bool {
var stack []int
n, idx, coach := len(coaches), 0, 1
here:
for {
switch {
case coach <= n && (len(stack) == 0 || coaches[idx] != stack[len(stack)-1]):
stack = append(stack, coach)
coach++
case len(stack) > 0 && coaches[idx] == stack[len(stack)-1]:
stack = stack[:len(stack)-1]
idx++
default:
break here
}
}
return len(stack) == 0 && idx == n
}
func main() {
in, _ := os.Open("514.in")
defer in.Close()
out, _ := os.Create("514.out")
defer out.Close()
var n int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
coaches := make([]int, n)
here:
for {
for i := range coaches {
fmt.Fscanf(in, "%d", &coaches[i])
if i == 0 && coaches[0] == 0 {
fmt.Fprintln(out)
break here
}
}
if solve(coaches) {
fmt.Fprintln(out, "Yes")
} else {
fmt.Fprintln(out, "No")
}
}
}
}