-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10020.go
71 lines (63 loc) · 1.16 KB
/
10020.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
// UVa 10020 - Minimal coverage
package main
import (
"fmt"
"io"
"os"
"sort"
)
func solve(l, r int, lines [][2]int) [][2]int {
maxRight := l
idx := -1
for i, v := range lines {
if v[0] > l {
break
}
if v[1] > maxRight {
maxRight = v[1]
idx = i
}
}
var ret [][2]int
if idx == -1 {
return ret
}
ret = append(ret, lines[idx])
if maxRight < r {
tmp := solve(maxRight, r, lines[idx+1:])
if len(tmp) == 0 {
return tmp
}
ret = append(ret, tmp...)
}
return ret
}
func output(out io.Writer, r [][2]int) {
fmt.Fprintln(out, len(r))
for _, v := range r {
fmt.Fprintln(out, v[0], v[1])
}
}
func main() {
in, _ := os.Open("10020.in")
defer in.Close()
out, _ := os.Create("10020.out")
defer out.Close()
var kase, m int
var line [2]int
for fmt.Fscanf(in, "%d\n", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "\n%d", &m)
var lines [][2]int
for {
if fmt.Fscanf(in, "%d%d", &line[0], &line[1]); line[0] == 0 && line[1] == 0 {
break
}
lines = append(lines, line)
}
sort.Slice(lines, func(i, j int) bool { return lines[i][0] < lines[j][0] })
output(out, solve(0, m, lines))
if kase > 1 {
fmt.Fprintln(out)
}
}
}