-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from wellFoundedDevelopers/heejik/60week
[장희직] - 트리의 지름, 공유기 설치
- Loading branch information
Showing
4 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Int> { | ||
val tmp = mutableListOf<Int>() | ||
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<Pair<Scves, Int>>() | ||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Char, Int>() | ||
val outOfAlpha = mutableMapOf<Char, MutableList<Int>>() | ||
|
||
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<Char, MutableList<Int>>() | ||
for ((alpha, outPrice) in outOfAlpha) { | ||
outOfAlphaInRange[alpha] = outPrice.toMutableList() | ||
} | ||
// println(outOfAlphaInRange) | ||
var price = 0 | ||
val trash = mutableListOf<Char>() | ||
// 먼저 쓰레기통에 다 넣는다. | ||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package heejik.`60week` | ||
|
||
class `트리의 지름` { | ||
|
||
fun solve() { | ||
val n = readln().toInt() | ||
|
||
val nodes = List(size = n + 1) { mutableListOf<Pair<Int, Int>>() } | ||
val queue = ArrayDeque<Pair<Int, Int>>() | ||
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<Int, Int> { | ||
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() | ||
} |