-
Notifications
You must be signed in to change notification settings - Fork 27
/
10131.go
85 lines (76 loc) · 1.44 KB
/
10131.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
// UVa 10131 - Is Bigger Smarter?
package main
import (
"fmt"
"io"
"os"
"sort"
)
type node struct{ w, s int }
func lis(nodes []node) ([]int, int) {
sort.Slice(nodes, func(i, j int) bool {
if nodes[i].w != nodes[j].w {
return nodes[i].w < nodes[j].w
}
return nodes[i].s > nodes[j].s
})
l := len(nodes)
dp := make([]int, l+1)
for i := range dp {
dp[i] = 1
}
pre, max := make([]int, l), 0
for i := 1; i < l; i++ {
for j := i + 1; j <= l; j++ {
if nodes[j-1].w > nodes[i-1].w && nodes[j-1].s < nodes[i-1].s {
if dp[j] < dp[i]+1 {
dp[j] = dp[i] + 1
pre[j-1] = i - 1
if dp[j] > max {
max = dp[j]
}
}
}
}
}
return pre, max
}
func getResult(pre []int, max int, nodex []node) []node {
res := make([]node, max)
for i := len(pre) - 1; i >= 0; i-- {
if pre[i] == max-1 {
max--
res[max] = nodex[i]
}
}
return res
}
func output(out io.Writer, res, orig []node) {
fmt.Fprintln(out, len(res))
for i := range res {
for j := range orig {
if res[i] == orig[j] {
fmt.Fprintln(out, j+1)
break
}
}
}
}
func main() {
in, _ := os.Open("10131.in")
defer in.Close()
out, _ := os.Create("10131.out")
defer out.Close()
var nodes []node
var n node
for {
if _, err := fmt.Fscanf(in, "%d%d", &n.w, &n.s); err != nil {
break
}
nodes = append(nodes, n)
}
orig := make([]node, len(nodes))
copy(orig, nodes)
pre, max := lis(nodes)
output(out, getResult(pre, max, nodes), orig)
}