結果

問題 No.4 おもりと天秤
ユーザー komkomhkomkomh
提出日時 2019-04-06 21:31:28
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 718 ms / 5,000 ms
コード長 1,207 bytes
コンパイル時間 18,202 ms
コンパイル使用メモリ 418,260 KB
実行使用メモリ 67,064 KB
最終ジャッジ日時 2023-08-13 01:37:30
合計ジャッジ時間 22,593 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 304 ms
53,004 KB
testcase_01 AC 300 ms
53,032 KB
testcase_02 AC 306 ms
53,072 KB
testcase_03 AC 300 ms
53,008 KB
testcase_04 AC 295 ms
52,936 KB
testcase_05 AC 306 ms
52,884 KB
testcase_06 AC 295 ms
52,932 KB
testcase_07 AC 311 ms
53,112 KB
testcase_08 AC 300 ms
52,904 KB
testcase_09 AC 303 ms
52,980 KB
testcase_10 AC 298 ms
53,296 KB
testcase_11 AC 292 ms
52,876 KB
testcase_12 AC 295 ms
52,872 KB
testcase_13 AC 303 ms
53,008 KB
testcase_14 AC 298 ms
52,928 KB
testcase_15 AC 302 ms
52,832 KB
testcase_16 AC 294 ms
53,116 KB
testcase_17 AC 297 ms
53,264 KB
testcase_18 AC 718 ms
67,064 KB
testcase_19 AC 301 ms
52,968 KB
testcase_20 AC 304 ms
52,944 KB
testcase_21 AC 304 ms
52,896 KB
testcase_22 AC 303 ms
53,216 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