-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path394.go
64 lines (56 loc) · 1.4 KB
/
394.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
// UVa 394 - Mapmaker
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type (
dimension struct{ lower, upper int }
array struct {
base, bytes, dim int
dimensions []dimension
}
)
func solve(line string, arrays map[string]array) string {
var name string
r := strings.NewReader(line)
fmt.Fscanf(r, "%s", &name)
arr := arrays[name]
dims := make([]int, arr.dim)
for i := range dims {
fmt.Fscanf(r, "%d", &dims[i])
}
base, count := 1, 0
for i := len(dims) - 1; i >= 0; i-- {
count += base * (dims[i] - arr.dimensions[i].lower)
base *= arr.dimensions[i].upper - arr.dimensions[i].lower + 1
}
return fmt.Sprintf("%s%s = %d", name, strings.Replace(fmt.Sprint(dims), " ", ", ", -1), arr.base+count*arr.bytes)
}
func main() {
in, _ := os.Open("394.in")
defer in.Close()
out, _ := os.Create("394.out")
defer out.Close()
var n, r int
var name string
fmt.Fscanf(in, "%d%d", &n, &r)
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
arrays := make(map[string]array)
for ; n > 0 && s.Scan(); n-- {
var arr array
r := strings.NewReader(s.Text())
fmt.Fscanf(r, "%s%d%d%d", &name, &arr.base, &arr.bytes, &arr.dim)
arr.dimensions = make([]dimension, arr.dim)
for i := range arr.dimensions {
fmt.Fscanf(r, "%d%d", &arr.dimensions[i].lower, &arr.dimensions[i].upper)
}
arrays[name] = arr
}
for ; r > 0 && s.Scan(); r-- {
fmt.Fprintln(out, solve(s.Text(), arrays))
}
}