結果

問題 No.4 おもりと天秤
ユーザー komkomhkomkomh
提出日時 2019-04-06 21:31:28
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 771 ms / 5,000 ms
コード長 1,207 bytes
コンパイル時間 13,477 ms
コンパイル使用メモリ 445,144 KB
実行使用メモリ 100,192 KB
最終ジャッジ日時 2024-04-30 20:11:12
合計ジャッジ時間 21,355 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 305 ms
57,016 KB
testcase_01 AC 301 ms
56,840 KB
testcase_02 AC 310 ms
56,940 KB
testcase_03 AC 305 ms
57,020 KB
testcase_04 AC 307 ms
57,144 KB
testcase_05 AC 321 ms
57,100 KB
testcase_06 AC 307 ms
56,888 KB
testcase_07 AC 316 ms
57,036 KB
testcase_08 AC 308 ms
57,072 KB
testcase_09 AC 322 ms
57,212 KB
testcase_10 AC 316 ms
57,044 KB
testcase_11 AC 306 ms
56,956 KB
testcase_12 AC 305 ms
57,040 KB
testcase_13 AC 307 ms
57,092 KB
testcase_14 AC 302 ms
56,976 KB
testcase_15 AC 305 ms
56,968 KB
testcase_16 AC 302 ms
56,968 KB
testcase_17 AC 305 ms
56,964 KB
testcase_18 AC 771 ms
100,192 KB
testcase_19 AC 308 ms
57,088 KB
testcase_20 AC 308 ms
57,008 KB
testcase_21 AC 309 ms
57,004 KB
testcase_22 AC 308 ms
57,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder

fun main() {
    val N: Int = readLine()!!.toInt()
    val W: List<Int> = readLine()!!.split(" ").map(String::toInt)

    val sum: Int = W.sum()
    if (sum % 2 != 0) {
        println("impossible")
        return
    }

    val halfW = sum / 2
    var cache: MutableMap<Pair<Int, Int>, Boolean> = mutableMapOf()
    fun search2(nokori: Int, index: Int): Boolean {
        val currentNokori = nokori - W[index]
        return when {
            currentNokori < 0 -> false
            currentNokori == 0 -> true
            else -> {
                for (i in index + 1 until N) {
                    val key = Pair(currentNokori, i)
                    val result = cache.get(key)?.let {
                        it
                    } ?: run {
                        val result = search2(currentNokori, i)
                        cache.put(key, result)
                        result
                    }
                    if (result) {
                        return result
                    }
                }
                false
            }
        }
    }

    when (search2(halfW, 0)) {
        true -> println("possible")
        false -> println("impossible")
    }
}
0