complete 1149.kt and 1463.kt
This commit is contained in:
32
zeta_kotlin/completed/1149.kt
Normal file
32
zeta_kotlin/completed/1149.kt
Normal 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))
|
||||||
|
}
|
||||||
24
zeta_kotlin/completed/1463.kt
Normal file
24
zeta_kotlin/completed/1463.kt
Normal 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));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user