結果

問題 No.3 ビットすごろく
ユーザー da_louis
提出日時 2020-02-12 01:01:41
言語 Kotlin
(2.1.0)
結果
WA  
実行時間 -
コード長 1,014 bytes
コンパイル時間 14,829 ms
コンパイル使用メモリ 445,096 KB
実行使用メモリ 56,548 KB
最終ジャッジ日時 2024-10-01 14:10:14
合計ジャッジ時間 26,976 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 WA * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:4:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^

ソースコード

diff #

import java.util.*
import kotlin.math.min

fun main(args: Array<String>) {
    p11()
}

fun printString(builderAction: StringBuilder.() -> Unit) = print(buildString(builderAction))

fun p11() = printString {
    val n = readLine()!!.toInt()

    val inf = Int.MAX_VALUE / 100
    val dp = IntArray(101010) { inf }
    dp[1] = 1

    fun recUpdate(value: Int) {
        if (value !in 1..n) return
        val move1 = BitSet.valueOf(longArrayOf(value.toLong())).cardinality()
        dp[value + move1] = min(dp[value + move1], dp[value] + 1)
        recUpdate(value + move1)
    }

    for (i in 1..n) {
        val move = BitSet.valueOf(longArrayOf(i.toLong())).cardinality()
        val left = i - move
        val right = i + move
        val needReCalc = dp[left] == inf
        dp[right] = min(dp[right], dp[i] + 1)
        dp[left] = min(dp[left], dp[i] + 1)
        if (needReCalc) {
            recUpdate(left)
        }
    }

    val answer = dp[n].let { if (it == inf) -1 else it }

    println(answer)
}
0