結果

問題 No.15 カタログショッピング
ユーザー バカらっくバカらっく
提出日時 2019-08-31 21:25:14
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 1,573 bytes
コンパイル時間 14,632 ms
コンパイル使用メモリ 429,652 KB
実行使用メモリ 86,676 KB
最終ジャッジ日時 2023-08-16 18:34:12
合計ジャッジ時間 24,443 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 258 ms
56,212 KB
testcase_01 AC 261 ms
86,676 KB
testcase_02 AC 268 ms
53,220 KB
testcase_03 AC 307 ms
57,228 KB
testcase_04 AC 272 ms
57,068 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^
Main.kt:36:38: warning: 'sumBy((T) -> Int): Int' is deprecated. Use sumOf instead.
    if(subTotal + itemList.drop(idx).sumBy { it.value } < total) {
                                     ^

ソースコード

diff #

import java.lang.StringBuilder

fun main(args: Array<String>) {
    val(n,s) = readLine()!!.trim().split(" ").map { it.toInt() }
    total = s
    for(i in 1..n) {
        itemList.add(Item(i-1, readLine()!!.toInt()))
    }
    itemList.sortByDescending { it.value }
    val ans = getAns(0, 0).sortedDescending()
    ans.forEach { println(convAns(it)) }
}

fun convAns(num:Int):String {
    val ans = StringBuilder()
    for(i in itemList.indices) {
        val key = 1 shl (itemList.lastIndex - i)
        if(num and key > 0) {
            ans.append(" ")
            ans.append(i + 1)
        }
    }
    return ans.toString().substring(1)
}

var total = 0
class Item(val index:Int, val value:Int)
val itemList = mutableListOf<Item>()

val dic = mutableMapOf<Int, MutableMap<Int, List<Int>>>()

fun getAns(idx:Int, subTotal:Int):List<Int> {
    if(subTotal > total) {
        return mutableListOf()
    }
    if(subTotal + itemList.drop(idx).sumBy { it.value } < total) {
        return mutableListOf()
    }
    if(subTotal == total) {
        return mutableListOf(0)
    }
    if(idx > itemList.lastIndex) {
        return mutableListOf()
    }
    if(!dic.containsKey(idx)) {
        dic[idx] = mutableMapOf()
    }
    dic[idx]!![subTotal]?.let { return it }

    val ans = mutableListOf<Int>()
    val ret1 = getAns(idx + 1, subTotal + itemList[idx].value)
    val ret2 = getAns(idx + 1, subTotal)
    val key = 1 shl (itemList.lastIndex - itemList[idx].index)
    ans.addAll(ret1.map { it + key })
    ans.addAll(ret2)
    dic[idx]!![subTotal]= ans
    return ans
}
0