-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path538.go
70 lines (64 loc) · 1.36 KB
/
538.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
// UVa 538 - Balancing Bank Accounts
package main
import (
"fmt"
"io"
"os"
)
type traveller struct {
name string
balance int
}
func solve(out io.Writer, travellers []traveller) {
for {
var max, min, idx1, idx2 int
for i, t := range travellers {
if t.balance > max {
max, idx1 = t.balance, i
}
if t.balance < min {
min, idx2 = t.balance, i
}
}
if max == 0 {
break
}
fmt.Fprintf(out, "%s %s ", travellers[idx1].name, travellers[idx2].name)
if max > -min {
fmt.Fprintln(out, -min)
travellers[idx1].balance += min
travellers[idx2].balance = 0
} else {
fmt.Fprintln(out, max)
travellers[idx1].balance = 0
travellers[idx2].balance += max
}
}
}
func main() {
in, _ := os.Open("538.in")
defer in.Close()
out, _ := os.Create("538.out")
defer out.Close()
var n, t, amount int
var t1, t2 string
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d%d", &n, &t); n == 0 && t == 0 {
break
}
nameMap := make(map[string]int)
travellers := make([]traveller, n)
for i := range travellers {
fmt.Fscanf(in, "%s", &travellers[i].name)
nameMap[travellers[i].name] = i
}
for ; t > 0; t-- {
fmt.Fscanf(in, "%s%s%d", &t1, &t2, &amount)
travellers[nameMap[t1]].balance -= amount
travellers[nameMap[t2]].balance += amount
}
fmt.Fprintf(out, "Case #%d\n", kase)
solve(out, travellers)
fmt.Fprintln(out)
}
}