-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathschedule.go
153 lines (129 loc) · 3.04 KB
/
schedule.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
type Time struct {
hour, min int
}
func newTime(hour, min int) Time {
return Time{hour, min}
}
func cmp(t1, t2 Time) int {
if t1.hour < t2.hour {
return -1
} else if t1.hour > t2.hour {
return 1
} else {
if t1.min < t2.min {
return -1
} else if t1.min > t2.min {
return 1
} else {
return 0
}
}
}
func (time *Time) toString() string {
hour := time.hour
min := time.min
if min != 0 {
h := strconv.Itoa(hour)
m := strconv.Itoa(min)
return h + "." + m
} else {
h := strconv.Itoa(hour)
return h
}
}
type Class struct {
start, finish Time
}
func convertToTime(time string) Time {
if strings.Contains(time, ".") {
hourMin := strings.Split(time, ".")
hour, _ := strconv.Atoi(hourMin[0])
min, _ := strconv.Atoi(hourMin[1])
return newTime(hour, min)
} else {
hour, _ := strconv.Atoi(time)
return newTime(hour, 0)
}
}
func newClass(start, finish string) Class {
return Class{convertToTime(start), convertToTime(finish)}
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
var line string
// читаем количество занятий
var n int
scanner.Scan()
line = scanner.Text()
n, _ = strconv.Atoi(line)
// читаем время начала и конца занятий
classes := make([]Class, n)
for i := 0; i < n; i++ {
scanner.Scan()
row := scanner.Text()
startFinish := strings.Split(row, " ")
classes[i] = newClass(startFinish[0], startFinish[1])
}
// сортируем занятия по возрастанию времени конца, а если оно совпадает,
// то по возрастанию времени начала
sort.Slice(classes, func(i, j int) bool {
class1 := classes[i]
class2 := classes[j]
finish1 := class1.finish
finish2 := class2.finish
cmpFinish := cmp(finish1, finish2)
if cmpFinish < 0 {
return true
} else if cmpFinish > 0 {
return false
} else {
start1 := class1.start
start2 := class2.start
if cmp(start1, start2) < 0 {
return true
} else {
return false
}
}
})
// выбираем очередное занятие с самым ранним временем конца, которое
// не пересекается с уже выбранным занятием
var lastEnd Time
var solution []Class
solution = append(solution, classes[0])
lastEnd = classes[0].finish
for i := 1; i < n; i++ {
class := classes[i]
if cmp(class.start, lastEnd) >= 0 {
solution = append(solution, class)
lastEnd = class.finish
}
}
// печатаем оптимальное расписание
fmt.Println(len(solution))
printArray(solution)
}
func printArray(array []Class) {
if len(array) == 0 {
return
}
for i := 0; i < len(array)-1; i++ {
class := array[i]
start := class.start
finish := class.finish
fmt.Printf("%s %s\n", start.toString(), finish.toString())
}
class := array[len(array)-1]
start := class.start
finish := class.finish
fmt.Printf("%s %s\n", start.toString(), finish.toString())
}