-
[백준] 2805번: 나무 자르기 - 코틀린(kotlin)알고리즘/백준 2022. 11. 12. 00:01728x90
https://www.acmicpc.net/problem/2805
문제 해석
나무가 M미터 필요하다. 이때 나무를 자르기 위해 절단기의 높이 H를 설정해야 한다. 나무를 최소한으로 필요한 만큼만 가져가려고 합니다.
코드
fun main() { val (hasTreeCount, needTreeCount) = readLine()!!.split(" ").map { it.toInt() } val treeHeights = IntArray(hasTreeCount) var index = 0 readLine()!!.split(" ").map { treeHeights[index++] = it.toInt() } val maxHeight = treeHeights.maxOrNull()!! var left = 0 var right = maxHeight var answer = 0L while (left <= right) { val middle = (left + right) / 2 var curHasTreeCount = 0L for (curTree in 0 until hasTreeCount) { if (treeHeights[curTree] > middle) { curHasTreeCount += treeHeights[curTree].toLong() - middle.toLong() } } if (curHasTreeCount >= needTreeCount) { answer = middle.toLong() left = middle + 1 } else { right = middle - 1 } } println(answer) }
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 27866번: 문자와 문자열 - 코틀린(Kotlin) (0) 2023.10.01 [백준] 1654번: 랜선 자르기 - 코틀린(kotlin) (0) 2022.11.13 [백준] 1874번 : 스택 수열 - 코틀린(kotlin) (0) 2022.11.10 [백준] 4949번: 균형잡힌 세상 - 코틀린(kotlin) (0) 2022.11.09 [백준] 7568번: 덩치 - 코틀린(kotlin) (1) 2022.11.08