complete 1149.kt and 1463.kt

This commit is contained in:
2024-05-24 23:56:56 +09:00
parent 07f769683e
commit 8031759e0f
2 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
import kotlin.math.min
fun List<Int>.toTriple(): Triple<Int, Int, Int> {
return Triple(this[0], this[1], this[2])
}
fun solve(N: Int, C: Array<Triple<Int, Int, Int>>): Int {
val T = Array<Triple<Int, Int, Int>>(N) { Triple(0, 0, 0) }
T[0] = C[0]
for (i in 1 until N) {
T[i] = Triple(
min(T[i - 1].second, T[i - 1].third) + C[i].first,
min(T[i - 1].first, T[i - 1].third) + C[i].second,
min(T[i - 1].first, T[i - 1].second) + C[i].third
)
}
return minOf(T[N - 1].first, T[N - 1].second, T[N - 1].third)
}
fun main() {
val N = readln().toInt()
val C = Array<Triple<Int, Int, Int>>(N) {
readln().split(' ').map { s: String ->
s.toInt()
}.toTriple()
}
println(solve(N, C))
}

View File

@@ -0,0 +1,24 @@
fun bfs(N: Int): Int {
val D = ArrayDeque<Pair<Int, Int>>()
D.addLast(Pair(N, 0))
while (D.isNotEmpty()) {
val (now, step) = D.removeFirst()
if (now == 1) {
return step
}
if (now % 3 == 0) {
D.addLast(Pair(now / 3, step + 1))
}
if (now % 2 == 0) {
D.addLast(Pair(now / 2, step + 1))
}
D.addLast(Pair(now - 1, step + 1))
}
return -1
}
fun main() {
val N = readln().toInt()
println(bfs(N));
}