結果

問題 No.3 ビットすごろく
ユーザー rutilicus
提出日時 2020-08-30 22:22:54
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 363 ms / 5,000 ms
コード長 879 bytes
コンパイル時間 13,467 ms
コンパイル使用メモリ 439,856 KB
実行使用メモリ 57,716 KB
最終ジャッジ日時 2024-07-01 09:55:16
合計ジャッジ時間 24,243 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:29:13: warning: 'appendln(Int): kotlin.text.StringBuilder /* = java.lang.StringBuilder */' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
    builder.appendln(if (dp[n] == Int.MAX_VALUE) -1 else dp[n])
            ^

ソースコード

diff #

import java.util.ArrayDeque

fun main() {
    val builder = StringBuilder()

    val n = readInputLine().toInt()

    val queue = ArrayDeque<Int>()

    val dp = IntArray(n + 1) { Int.MAX_VALUE }

    dp[1] = 1
    queue.add(1)

    while (queue.isNotEmpty()) {
        val current = queue.poll()
        val bitCnt = current.toString(2).count { it == '1' }

        if (current - bitCnt > 0 && dp[current - bitCnt] > dp[current] + 1) {
            dp[current - bitCnt] = dp[current] + 1
            queue.add(current - bitCnt)
        }
        if (current + bitCnt <= n && dp[current + bitCnt] > dp[current] + 1) {
            dp[current + bitCnt] = dp[current] + 1
            queue.add(current + bitCnt)
        }
    }

    builder.appendln(if (dp[n] == Int.MAX_VALUE) -1 else dp[n])

    print(builder.toString())
}

fun readInputLine(): String {
    return readLine()!!
}
0