結果

問題 No.3 ビットすごろく
コンテスト
ユーザー rutilicus
提出日時 2020-08-30 22:22:54
言語 Kotlin
(2.3.10)
コンパイル:
kotlinc _filename_ -include-runtime -d main.jar
実行:
kotlin main.jar
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 879 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 8,361 ms
コンパイル使用メモリ 413,748 KB
最終ジャッジ日時 2026-03-22 07:44:52
合計ジャッジ時間 8,833 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
Main.kt:29:13: error: 'fun StringBuilder.appendln(value: Int): 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 #
raw source code

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