-
Notifications
You must be signed in to change notification settings - Fork 27
/
617.go
79 lines (72 loc) · 1.38 KB
/
617.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
// UVa 617 - Nonstop Travel
package main
import (
"fmt"
"io"
"os"
)
type light struct {
distance float64
g, y, r int
}
func solve(lights []light) []int {
var speeds []int
here:
for speed := 30; speed <= 60; speed++ {
for _, l := range lights {
if int(l.distance/float64(speed)*3600)%(l.g+l.y+l.r) > l.g+l.y {
continue here
}
}
speeds = append(speeds, speed)
}
return speeds
}
func output(out io.Writer, speeds []int) {
if len(speeds) == 0 {
fmt.Fprintln(out, "No acceptable speeds.")
return
}
var rangeStarted bool
for i, speed := range speeds {
if i == 0 {
fmt.Fprint(out, speed)
continue
}
if speed == speeds[i-1]+1 {
if !rangeStarted {
rangeStarted = true
fmt.Fprint(out, "-")
}
if i == len(speeds)-1 {
fmt.Fprint(out, speed)
}
} else {
if rangeStarted {
rangeStarted = false
fmt.Fprintf(out, "%d", speeds[i-1])
}
fmt.Fprintf(out, ", %d", speed)
}
}
fmt.Fprintln(out)
}
func main() {
in, _ := os.Open("617.in")
defer in.Close()
out, _ := os.Create("617.out")
defer out.Close()
var n int
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d", &n); n == -1 {
break
}
fmt.Fprintf(out, "Case %d: ", kase)
lights := make([]light, n)
for i := range lights {
fmt.Fscanf(in, "%f%d%d%d", &lights[i].distance, &lights[i].g, &lights[i].y, &lights[i].r)
}
output(out, solve(lights))
fmt.Fscanln(in)
}
}