-
Notifications
You must be signed in to change notification settings - Fork 27
/
10534.go
80 lines (71 loc) · 1.33 KB
/
10534.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
80
// UVa 10534 - Wavio Sequence
package main
import (
"fmt"
"math"
"os"
)
var n int
func binarySearch(num int, stack []int) int {
low, high := 0, len(stack)-1
for low <= high {
if mid := (low + high) / 2; num > stack[mid] {
low = mid + 1
} else {
high = mid - 1
}
}
return low
}
func lis(num []int) []int {
dp := make([]int, n)
stack := []int{math.MinInt32}
for i := range num {
if num[i] > stack[len(stack)-1] {
stack = append(stack, num[i])
} else {
stack[binarySearch(num[i], stack)] = num[i]
}
dp[i] = len(stack) - 1
}
return dp
}
func lds(num []int) []int {
dp := make([]int, n)
stack := []int{math.MinInt32}
for i := n - 1; i >= 0; i-- {
if num[i] > stack[len(stack)-1] {
stack = append(stack, num[i])
} else {
stack[binarySearch(num[i], stack)] = num[i]
}
dp[i] = len(stack) - 1
}
return dp
}
func solve(num []int) int {
dp1, dp2 := lis(num), lds(num)
var length int
for i := range num {
if tmp := min(dp1[i], dp2[i]); length < tmp {
length = tmp
}
}
return 2*length - 1
}
func main() {
in, _ := os.Open("10534.in")
defer in.Close()
out, _ := os.Create("10534.out")
defer out.Close()
for {
if _, err := fmt.Fscanf(in, "%d", &n); err != nil {
break
}
num := make([]int, n)
for i := range num {
fmt.Fscanf(in, "%d", &num[i])
}
fmt.Fprintln(out, solve(num))
}
}