結果

問題 No.3 ビットすごろく
ユーザー rutilicusrutilicus
提出日時 2020-08-30 22:22:54
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 345 ms / 5,000 ms
コード長 879 bytes
コンパイル時間 15,916 ms
コンパイル使用メモリ 424,460 KB
実行使用メモリ 53,464 KB
最終ジャッジ日時 2023-09-14 01:50:45
合計ジャッジ時間 26,352 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 291 ms
52,688 KB
testcase_01 AC 291 ms
52,620 KB
testcase_02 AC 288 ms
52,636 KB
testcase_03 AC 301 ms
53,116 KB
testcase_04 AC 298 ms
53,348 KB
testcase_05 AC 308 ms
53,136 KB
testcase_06 AC 311 ms
52,804 KB
testcase_07 AC 299 ms
52,888 KB
testcase_08 AC 309 ms
52,916 KB
testcase_09 AC 318 ms
52,824 KB
testcase_10 AC 321 ms
52,924 KB
testcase_11 AC 318 ms
53,112 KB
testcase_12 AC 315 ms
53,020 KB
testcase_13 AC 305 ms
53,000 KB
testcase_14 AC 322 ms
53,036 KB
testcase_15 AC 345 ms
53,464 KB
testcase_16 AC 343 ms
53,464 KB
testcase_17 AC 342 ms
53,352 KB
testcase_18 AC 300 ms
52,772 KB
testcase_19 AC 342 ms
53,448 KB
testcase_20 AC 298 ms
52,916 KB
testcase_21 AC 288 ms
52,852 KB
testcase_22 AC 323 ms
52,856 KB
testcase_23 AC 342 ms
53,352 KB
testcase_24 AC 342 ms
53,376 KB
testcase_25 AC 339 ms
53,360 KB
testcase_26 AC 292 ms
52,912 KB
testcase_27 AC 299 ms
52,940 KB
testcase_28 AC 322 ms
53,172 KB
testcase_29 AC 315 ms
53,032 KB
testcase_30 AC 292 ms
53,108 KB
testcase_31 AC 291 ms
52,908 KB
testcase_32 AC 317 ms
53,012 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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