-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path481.go
79 lines (71 loc) · 1.41 KB
/
481.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// UVa 481 - What Goes Up
package main
import (
"fmt"
"io"
"os"
)
type node struct{ num, idx int }
func binarySearch(n int, l [][]node, s, e int) int {
// find the 1st index that is not less than n
// for build-in binary search, see UVa 497
m := (s + e) / 2
switch mid := l[m]; {
case n == mid[len(mid)-1].num:
return m
case s == e:
return s
case n > mid[len(mid)-1].num:
return binarySearch(n, l, m+1, e)
default:
return binarySearch(n, l, s, m)
}
}
func lis(l []int) [][]node {
t := []node{{l[0], 0}}
s := [][]node{t}
for i := 1; i < len(l); i++ {
if last := s[len(s)-1]; l[i] > last[len(last)-1].num {
t = []node{{l[i], i}}
s = append(s, t)
} else {
idx := binarySearch(l[i], s, 0, len(s))
t = s[idx]
t = append(t, node{l[i], i})
s[idx] = t
}
}
return s
}
func output(out io.Writer, l []int) {
fmt.Fprintf(out, "%d\n-\n", len(l))
for i := len(l) - 1; i >= 0; i-- {
fmt.Fprintf(out, "%d\n", l[i])
}
}
func main() {
in, _ := os.Open("481.in")
defer in.Close()
out, _ := os.Create("481.out")
defer out.Close()
var tmp int
var l []int
for {
if _, err := fmt.Fscanf(in, "%d", &tmp); err != nil {
break
}
l = append(l, tmp)
}
s, idx := lis(l), len(l)
var lst []int
for i := len(s) - 1; i >= 0; i-- {
for j := len(s[i]) - 1; j >= 0; j-- {
if s[i][j].idx < idx {
lst = append(lst, s[i][j].num)
idx = s[i][j].idx
break
}
}
}
output(out, lst)
}