-
Notifications
You must be signed in to change notification settings - Fork 1
/
minimum_time_required.go
75 lines (59 loc) · 1.4 KB
/
minimum_time_required.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
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
func minimumTime(machines []int64, goal int64) int64 {
sort.Slice(machines, func(i, j int) bool { return machines[i] < machines[j] })
slowestMachine, fastestMachine, n := machines[0], machines[len(machines)-1], int64(len(machines))
lowerBound := goal * slowestMachine / n
upperBound := goal * fastestMachine / n
for lowerBound < upperBound {
numDays := (lowerBound + upperBound) >> 1
if total := getNumItems(machines, numDays); total >= goal {
upperBound = numDays
} else {
lowerBound = numDays + 1
}
}
return lowerBound
}
func getNumItems(machines []int64, numDays int64) int64 {
var result int64
for _, machine := range machines {
result += numDays / machine
}
return result
}
func main() {
stdin, err := os.Open(os.Getenv("INPUT_PATH"))
if err != nil {
stdin = os.Stdin
}
defer stdin.Close()
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
if err != nil {
stdout = os.Stdout
}
defer stdout.Close()
reader := bufio.NewReaderSize(stdin, 1024*1024)
writer := bufio.NewWriterSize(stdout, 1024*1024)
var n int
var goal int64
fmt.Fscan(reader, &n, &goal)
machines := make([]int64, n)
for i := 0; i < n; i++ {
_, err = fmt.Fscan(reader, &machines[i])
checkError(err)
}
answer := minimumTime(machines, goal)
fmt.Fprint(writer, answer)
writer.Flush()
}
func checkError(err error) {
if err != nil {
panic(err)
}
}