-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path103.go
109 lines (100 loc) · 1.84 KB
/
103.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// UVa 103 - Stacking Boxes
package main
import (
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
)
func fit(a, b []int) bool {
for i := range a {
if a[i] >= b[i] {
return false
}
}
return true
}
func lis(n int, boxes [][]int) []int {
sort.Slice(boxes, func(i, j int) bool {
for idx := range boxes[i] {
if boxes[i][idx] != boxes[j][idx] {
return boxes[i][idx] < boxes[j][idx]
}
}
return false
})
length := make([]int, n)
for i := range length {
length[i] = 1
}
pre := make([]int, n)
for i := range pre {
pre[i] = -1
}
max, maxIdx := 0, 0
for i := 0; i < n-1; i++ {
for j := i + 1; j < n; j++ {
if fit(boxes[i], boxes[j]) {
if length[i]+1 > length[j] {
if length[j] = length[i] + 1; length[j] > max {
max = length[j]
maxIdx = j
}
pre[j] = i
}
}
}
}
ret := []int{maxIdx}
for pre[maxIdx] != -1 {
maxIdx = pre[maxIdx]
ret = append([]int{maxIdx}, ret...)
}
return ret
}
func isSame(a, b []int) bool {
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func output(out io.Writer, order []int, box, original [][]int) {
var ret []string
for _, v := range order {
for j := range original {
if isSame(box[v], original[j]) {
ret = append(ret, strconv.Itoa(j+1))
break
}
}
}
fmt.Fprintln(out, len(ret))
fmt.Fprintln(out, strings.Join(ret, " "))
}
func main() {
in, _ := os.Open("103.in")
defer in.Close()
out, _ := os.Create("103.out")
defer out.Close()
var n, d int
for {
if _, err := fmt.Fscanf(in, "%d%d", &n, &d); err != nil {
break
}
boxes := make([][]int, n)
for i := range boxes {
boxes[i] = make([]int, d)
for j := range boxes[i] {
fmt.Fscanf(in, "%d", &boxes[i][j])
}
sort.Ints(boxes[i])
}
original := make([][]int, n)
copy(original, boxes)
output(out, lis(n, boxes), boxes, original)
}
}