-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path699.go
More file actions
74 lines (63 loc) · 1.08 KB
/
699.go
File metadata and controls
74 lines (63 loc) · 1.08 KB
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
// UVa 699 - The Falling Leaves
package main
import (
"fmt"
"io"
"os"
)
const max = 200
type node struct {
n int
left, right *node
}
var (
low, high int
sums []int
in io.ReadCloser
)
func nextNumber() int {
var n int
fmt.Fscanf(in, "%d", &n)
return n
}
func sumTree(root *node, idx int) {
if root != nil {
if idx < low {
low = idx
}
if idx > high {
high = idx
}
sums[idx] += root.n
sumTree(root.left, idx-1)
sumTree(root.right, idx+1)
}
}
func buildTree(n int) *node {
if n != -1 {
return &node{n, buildTree(nextNumber()), buildTree(nextNumber())}
}
return nil
}
func solve(n int) []int {
root := buildTree(n)
low, high = max-1, 0
sums = make([]int, max)
sumTree(root, max/2)
return sums[low : high+1]
}
func main() {
in, _ = os.Open("699.in")
defer in.Close()
out, _ := os.Create("699.out")
defer out.Close()
var n int
for kase := 1; ; kase++ {
if n = nextNumber(); n == -1 {
break
}
fmt.Fprintf(out, "Case %d:\n", kase)
output := fmt.Sprint(solve(n))
fmt.Fprintf(out, "%s\n\n", output[1:len(output)-1])
}
}