-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10191.go
64 lines (56 loc) · 1.33 KB
/
10191.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
// UVa 10191 - Longest Nap
package main
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"strconv"
)
type job struct{ start, end int }
func find(jobs []job) (int, int) {
var start, period, max int
t := 10 * 60
for _, j := range jobs {
if period = j.start - t; period > max {
max = period
start = t
}
t = j.end
}
if period = 18*60 - t; period > max {
max = period
start = t
}
return max, start
}
func output(out io.Writer, kase, max, start int) {
var duration string
if max < 60 {
duration = strconv.Itoa(max) + " minutes"
} else {
duration = fmt.Sprintf("%d hours and %d minutes", max/60, max%60)
}
startStr := fmt.Sprintf("%02d:%02d", start/60, start%60)
fmt.Fprintf(out, "Day #%d: the longest nap starts at %s and will last for %s.\n", kase, startStr, duration)
}
func main() {
in, _ := os.Open("10191.in")
defer in.Close()
out, _ := os.Create("10191.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var n, h1, m1, h2, m2 int
for kase := 1; s.Scan(); kase++ {
var js []job
for fmt.Sscanf(s.Text(), "%d", &n); n > 0 && s.Scan(); n-- {
fmt.Sscanf(s.Text(), "%d:%d%d:%d", &h1, &m1, &h2, &m2)
js = append(js, job{h1*60 + m1, h2*60 + m2})
}
sort.Slice(js, func(i, j int) bool { return js[i].start < js[j].start })
max, start := find(js)
output(out, kase, max, start)
}
}