結果

問題 No.4 おもりと天秤
ユーザー バカらっくバカらっく
提出日時 2019-08-29 08:01:53
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 626 ms / 5,000 ms
コード長 899 bytes
コンパイル時間 12,982 ms
コンパイル使用メモリ 444,556 KB
実行使用メモリ 88,160 KB
最終ジャッジ日時 2024-04-30 21:25:44
合計ジャッジ時間 24,299 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 325 ms
56,984 KB
testcase_01 AC 334 ms
56,956 KB
testcase_02 AC 337 ms
57,184 KB
testcase_03 AC 338 ms
57,108 KB
testcase_04 AC 342 ms
57,168 KB
testcase_05 AC 608 ms
85,844 KB
testcase_06 AC 329 ms
56,996 KB
testcase_07 AC 614 ms
88,160 KB
testcase_08 AC 435 ms
64,108 KB
testcase_09 AC 438 ms
65,220 KB
testcase_10 AC 626 ms
88,064 KB
testcase_11 AC 326 ms
56,836 KB
testcase_12 AC 327 ms
56,904 KB
testcase_13 AC 326 ms
56,848 KB
testcase_14 AC 332 ms
56,892 KB
testcase_15 AC 324 ms
56,932 KB
testcase_16 AC 329 ms
56,876 KB
testcase_17 AC 324 ms
56,964 KB
testcase_18 AC 525 ms
76,780 KB
testcase_19 AC 584 ms
86,208 KB
testcase_20 AC 596 ms
86,040 KB
testcase_21 AC 581 ms
83,932 KB
testcase_22 AC 588 ms
83,868 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:1:10: warning: parameter 'args' is never used
fun main(args: Array<String>){
         ^
Main.kt:2:9: warning: variable 'weightCount' is never used
    val weightCount = readLine()!!.toInt()
        ^
Main.kt:4:30: warning: 'sumBy((T) -> Int): Int' is deprecated. Use sumOf instead.
    totalWeight = weightList.sumBy { it }
                             ^

ソースコード

diff #

fun main(args: Array<String>){
    val weightCount = readLine()!!.toInt()
    weightList = readLine()!!.split(" ").map { it.toInt() }
    totalWeight = weightList.sumBy { it }
    val ans = if(getAns(0, 0)) "possible" else "impossible"
    println(ans)
}

var weightList = listOf<Int>()
var totalWeight = 0
val dic = mutableMapOf<Int, MutableMap<Int, Boolean>>()

fun getAns(pos:Int, subTotal:Int):Boolean {
    if(subTotal * 2 == totalWeight) {
        return true
    }
    if(subTotal * 2 > totalWeight) {
        return false
    }
    if(pos > weightList.lastIndex) {
        return false
    }
    if(!dic.containsKey(pos)) {
        dic[pos] = mutableMapOf()
    }
    dic[pos]!![subTotal]?.let {
        return it
    }

    val ret1 = getAns(pos + 1, subTotal)
    val ret2 = getAns(pos + 1, subTotal + weightList[pos])

    dic[pos]!![subTotal] = (ret1 || ret2)
    return (ret1 || ret2)
}
0