[백준/코틀린] 18111번: 마인크래프트
실버 2
링크
풀이
모든 가능한 높이(0~256)에 대해, 해당 높이로 ‘땅 고르기’가 가능한지 확인합니다.
코드
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
fun main() {
val (n, m, b) = readln().split(" ").map { it.toInt() }
val field = List(n) { readln().split(" ").map { it.toInt() } }.flatten()
var minTime = Int.MAX_VALUE
var maxHeight = -1
for (height in 0..256) {
var remove = 0
var add = 0
field.forEach {
when {
height < it -> remove += (it - height)
it < height -> add += (height - it)
}
}
if (b + remove >= add) {
val time = 2 * remove + add
if (time <= minTime) {
minTime = time
maxHeight = height
}
}
}
println("$minTime $maxHeight")
}
This post is licensed under CC BY 4.0 by the author.