From c7bd368652769ed677c1992be77ffa0286f78de8 Mon Sep 17 00:00:00 2001 From: jhg3410 Date: Mon, 29 Apr 2024 15:37:02 +0900 Subject: [PATCH 1/4] =?UTF-8?q?solve:=20=ED=8A=B8=EB=A6=AC=EC=9D=98=20?= =?UTF-8?q?=EC=A7=80=EB=A6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\235\230 \354\247\200\353\246\204.kt" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "src/main/kotlin/heejik/60week/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.kt" diff --git "a/src/main/kotlin/heejik/60week/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.kt" "b/src/main/kotlin/heejik/60week/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.kt" new file mode 100644 index 00000000..d126c023 --- /dev/null +++ "b/src/main/kotlin/heejik/60week/\355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.kt" @@ -0,0 +1,50 @@ +package heejik.`60week` + +class `트리의 지름` { + + fun solve() { + val n = readln().toInt() + + val nodes = List(size = n + 1) { mutableListOf>() } + val queue = ArrayDeque>() + val visited = BooleanArray(size = n + 1) + + repeat(n - 1) { + val (root, child, distance) = readln().split(' ').map { it.toInt() } + + nodes[root].add(child to distance) + nodes[child].add(root to distance) + } + + fun bfs(start: Int): Pair { + visited.fill(element = false) + // 원소, 가중치 + queue.add(start to 0) + visited[start] = true + var maxDis = 0 + var maxNumber = 0 + + while (queue.isNotEmpty()) { + val (number, sumDistance) = queue.removeFirst() + nodes[number].filter { visited[it.first].not() }.forEach { + queue.add(it.first to sumDistance + it.second) + visited[it.first] = true + } + if (sumDistance > maxDis) { + maxDis = sumDistance + maxNumber = number + } + } + + return maxNumber to maxDis + } + + val (number, _) = bfs(1) + println(bfs(number).second) + } +} + + +fun main() { + `트리의 지름`().solve() +} \ No newline at end of file From c5ba4b24e83d28b0c9cf99ee85f8ca7b78aaf8bb Mon Sep 17 00:00:00 2001 From: jhg3410 Date: Mon, 29 Apr 2024 17:26:20 +0900 Subject: [PATCH 2/4] =?UTF-8?q?solve:=20=EA=B3=B5=EC=9C=A0=EA=B8=B0=20?= =?UTF-8?q?=EC=84=A4=EC=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\352\270\260 \354\204\244\354\271\230.kt" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "src/main/kotlin/heejik/60week/\352\263\265\354\234\240\352\270\260 \354\204\244\354\271\230.kt" diff --git "a/src/main/kotlin/heejik/60week/\352\263\265\354\234\240\352\270\260 \354\204\244\354\271\230.kt" "b/src/main/kotlin/heejik/60week/\352\263\265\354\234\240\352\270\260 \354\204\244\354\271\230.kt" new file mode 100644 index 00000000..0c7afa8e --- /dev/null +++ "b/src/main/kotlin/heejik/60week/\352\263\265\354\234\240\352\270\260 \354\204\244\354\271\230.kt" @@ -0,0 +1,43 @@ +package heejik.`60week` + +class `공유기 설치` { + + fun solve() { + val (n, c) = readln().split(' ').map { it.toInt() } + val homes = MutableList(size = n) { 0L } + repeat(n) { + homes[it] = readln().toLong() + } + homes.sort() + + fun canInstall(distance: Long): Boolean { + var now = homes.first() + var installCount = 1 + homes.drop(1).forEach { pos -> + if (pos - now >= distance) { + installCount += 1 + now = pos + } + } + + return installCount >= c + } + + var start = 1L + var end: Long = homes.last() - homes.first() + + while (start <= end) { + val middle = (start + end) / 2 + if (canInstall(middle)) { // 설치가 가능하면 오른쪽을 본다. + start = middle + 1 + } else { // 설치가 안되면 왼쪽을 본다. + end = middle - 1 + } + } + println(end) + } +} + +fun main() { + `공유기 설치`().solve() +} \ No newline at end of file From 70cb17aa3381ba6dc26c479a1743ef143e446892 Mon Sep 17 00:00:00 2001 From: jhg3410 Date: Mon, 29 Apr 2024 19:51:46 +0900 Subject: [PATCH 3/4] =?UTF-8?q?solving:=20=EB=AE=A4=ED=83=88=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\353\246\254\354\212\244\355\201\254.kt" | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 "src/main/kotlin/heejik/60week/\353\256\244\355\203\210\353\246\254\354\212\244\355\201\254.kt" diff --git "a/src/main/kotlin/heejik/60week/\353\256\244\355\203\210\353\246\254\354\212\244\355\201\254.kt" "b/src/main/kotlin/heejik/60week/\353\256\244\355\203\210\353\246\254\354\212\244\355\201\254.kt" new file mode 100644 index 00000000..8b09dc04 --- /dev/null +++ "b/src/main/kotlin/heejik/60week/\353\256\244\355\203\210\353\246\254\354\212\244\355\201\254.kt" @@ -0,0 +1,88 @@ +package heejik.`60week` + +class 뮤탈리스크 { + + data class Scves( + val hp1: Int, + var hp2: Int, + var hp3: Int + ) { + fun isAllDie() = hp1 <= 0 && hp2 <= 0 && hp3 <= 0 + fun countLive(): Int { + var count = if (hp1 > 0) 1 else 0 + if (hp2 > 0) count += 1 + if (hp3 > 0) count += 1 + return count + } + + fun getLiveHP(): List { + val tmp = mutableListOf() + if (hp1 > 0) tmp.add(hp1) + if (hp2 > 0) tmp.add(hp2) + if (hp3 > 0) tmp.add(hp3) + return tmp + } + } + + + val cases = listOf( + listOf(9, 3, 1), + listOf(9, 1, 3), + listOf(3, 9, 1), + listOf(3, 1, 9), + listOf(1, 3, 9), + listOf(1, 9, 3) + ) + + fun solve() { + val n = readln().toInt() + val hps = readln().split(' ').map { it.toInt() } + val scves = Scves(hp1 = hps.first(), hp2 = hps.getOrElse(1) { 0 }, hp3 = hps.getOrElse(2) { 0 }) + val queue = ArrayDeque>() + queue.add(scves to 0) + + while (queue.isNotEmpty()) { + val nowHps = queue.removeFirst() + if (nowHps.first.isAllDie()) { + println(nowHps.second) + break + } + val liveCount = nowHps.first.countLive() + + if (liveCount == 1) { + queue.add( + nowHps.first.copy( + hp1 = nowHps.first.hp1 - 9, + hp2 = nowHps.first.hp2 - 9, + hp3 = nowHps.first.hp3 - 9, + ) to nowHps.second + 1 + ) + } else if (liveCount == 2) { + val (hp1, hp2) = nowHps.first.getLiveHP() + for ((case1, case2) in listOf((9 to 3), (3 to 9))) { + queue.add( + nowHps.first.copy( + hp1 = hp1 - case1, + hp2 = hp2 - case2, + hp3 = 0 + ) to nowHps.second + 1 + ) + } + } else { + for (case in cases) { + queue.add( + nowHps.first.copy( + hp1 = nowHps.first.hp1 - case[0], + hp2 = nowHps.first.hp2 - case[1], + hp3 = nowHps.first.hp3 - case[2], + ) to nowHps.second + 1 + ) + } + } + } + } +} + +fun main() { + 뮤탈리스크().solve() +} \ No newline at end of file From caed3685462b43a36f02efce69e7d2a6353ef17b Mon Sep 17 00:00:00 2001 From: jhg3410 Date: Mon, 29 Apr 2024 19:52:07 +0900 Subject: [PATCH 4/4] =?UTF-8?q?solve:=20=EC=8A=A4=ED=8B=B0=EC=BB=A4=20?= =?UTF-8?q?=EC=9E=AC=EB=B0=B0=EC=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \354\236\254\353\260\260\354\271\230.kt" | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 "src/main/kotlin/heejik/60week/\354\212\244\355\213\260\354\273\244 \354\236\254\353\260\260\354\271\230.kt" diff --git "a/src/main/kotlin/heejik/60week/\354\212\244\355\213\260\354\273\244 \354\236\254\353\260\260\354\271\230.kt" "b/src/main/kotlin/heejik/60week/\354\212\244\355\213\260\354\273\244 \354\236\254\353\260\260\354\271\230.kt" new file mode 100644 index 00000000..f44e7827 --- /dev/null +++ "b/src/main/kotlin/heejik/60week/\354\212\244\355\213\260\354\273\244 \354\236\254\353\260\260\354\271\230.kt" @@ -0,0 +1,96 @@ +package heejik.`60week` + +import kotlin.math.min + +class `스티커 재배치` { + + fun solve() { + var minPrice = Int.MAX_VALUE + + val (n, m, k) = readln().split(' ').map { it.toInt() } + // 각 인덱스마다 번호에 따른 알맞은 값 존재 + val stickerAlpha = MutableList(size = m) { 'a' } + val stickerOut = MutableList(size = m) { 0 } + val stickerPrice = MutableList(size = m) { 0 } + val priceOfAlpha = mutableMapOf() + val outOfAlpha = mutableMapOf>() + + repeat(m) { id -> + val (s, d, j) = readln().split(' ') + stickerAlpha[id] = s.first() + stickerOut[id] = d.toInt() + stickerPrice[id] = j.toInt() + // 각 알파벳마다 최소 가격 리스트업 + priceOfAlpha[s.first()] = min(priceOfAlpha.getOrDefault(s.first(), Int.MAX_VALUE), j.toInt()) + // 각 알파벳마다 최소 떼기 비용 리스트업 + outOfAlpha[s.first()]?.add(d.toInt()) ?: run { outOfAlpha[s.first()] = mutableListOf(d.toInt()) } + } +// println(outOfAlpha) +// println(outOfAlpha) +// println(cheapestPrice) + + val board = readln().split(' ').map { it.toInt() - 1 } + val s = readln() + + for (i in 0..board.size - s.length) { + val outOfAlphaInRange = mutableMapOf>() + for ((alpha, outPrice) in outOfAlpha) { + outOfAlphaInRange[alpha] = outPrice.toMutableList() + } +// println(outOfAlphaInRange) + var price = 0 + val trash = mutableListOf() + // 먼저 쓰레기통에 다 넣는다. + for (j in i until i + s.length) { + val indexOfS = j - i + val findSticker = s[indexOfS] + val currentSticker = stickerAlpha[board[j]] + // s 의 부분과 보드판 위의 알파벳의 부분과 다르면 + if (findSticker != currentSticker) { + // 현재 스티커 떼기 + price += stickerOut[board[j]] + // 쓰레기통에 넣기 + trash.add(currentSticker) + } + } + + // 쓰레기통에 넣은 걸 기반으로 재활용 하거나 구매 + for (j in i until i + s.length) { + val indexOfS = j - i + val findSticker = s[indexOfS] + val currentSticker = stickerAlpha[board[j]] +// println("$i, $j, findSticker: $findSticker, currentSticker, $currentSticker") + // s 의 부분과 보드판 위의 알파벳의 부분과 다르면 + if (findSticker != currentSticker) { + // 쓰레기통에 있다면 재활용 + if (findSticker in trash) { + trash.remove(findSticker) +// println("$i, $j, $price, 34343") + } else { // 쓰레기통에 없다면 해당 알파벳 최소 가격으로 구매 + if (findSticker !in priceOfAlpha) { + print(-1) + return + } + if (outOfAlphaInRange[findSticker]!!.min() < priceOfAlpha[findSticker]!!) { + val minOutPrice = outOfAlphaInRange[findSticker]!!.min() + outOfAlphaInRange[findSticker]!!.remove(minOutPrice) + price += minOutPrice + } else { + price += priceOfAlpha[findSticker]!! + } + } + } + } +// println(price) + if (price < minPrice) { + minPrice = price + } + } + + print(if (minPrice == Int.MAX_VALUE) -1 else minPrice) + } +} + +fun main() { + `스티커 재배치`().solve() +} \ No newline at end of file