-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10249.go
58 lines (52 loc) · 1.12 KB
/
10249.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
// UVa 10249 - The Grand Dinner
package main
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
)
type table struct{ no, size int }
func solve(m int, teams []int, tables []table) [][]string {
seats := make([][]string, m)
for i, team := range teams {
sort.Slice(tables, func(i, j int) bool { return tables[i].size > tables[j].size })
for j := 0; j < team; j++ {
if tables[j].size--; tables[j].size < 0 {
return nil
}
seats[i] = append(seats[i], strconv.Itoa(tables[j].no))
}
}
return seats
}
func main() {
in, _ := os.Open("10249.in")
defer in.Close()
out, _ := os.Create("10249.out")
defer out.Close()
var m, n int
for {
if fmt.Fscanf(in, "%d%d", &m, &n); m == 0 && n == 0 {
break
}
teams := make([]int, m)
for i := range teams {
fmt.Fscanf(in, "%d", &teams[i])
}
tables := make([]table, n)
for i := range tables {
fmt.Fscanf(in, "%d", &tables[i].size)
tables[i].no = i + 1
}
if seats := solve(m, teams, tables); seats != nil {
fmt.Fprintln(out, 1)
for _, seat := range seats {
fmt.Fprintln(out, strings.Join(seat, " "))
}
} else {
fmt.Fprintln(out, 0)
}
}
}