結果

問題 No.15 カタログショッピング
ユーザー バカらっくバカらっく
提出日時 2019-09-01 05:54:13
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 2,072 bytes
コンパイル時間 18,613 ms
コンパイル使用メモリ 430,732 KB
実行使用メモリ 101,660 KB
最終ジャッジ日時 2023-08-18 03:47:51
合計ジャッジ時間 32,837 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 297 ms
53,028 KB
testcase_01 AC 300 ms
53,068 KB
testcase_02 AC 314 ms
53,132 KB
testcase_03 AC 321 ms
53,152 KB
testcase_04 WA -
testcase_05 AC 2,038 ms
101,340 KB
testcase_06 AC 2,275 ms
99,388 KB
testcase_07 AC 2,056 ms
99,156 KB
testcase_08 AC 1,731 ms
100,836 KB
testcase_09 AC 2,101 ms
101,660 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^

ソースコード

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 zenhan = getHalfList(itemList.subList(0, itemList.size / 2), 0, 0)
    val kouhan = getHalfList(itemList.subList(itemList.size / 2, itemList.size), 0, 0)

    val ans = mutableListOf<Int>()

    zenhan.filter { it.key < total }.forEach{
        val key = total - it.key
        val zen = it.value
        kouhan[key]?.let {
            it.forEach {
                val kou = it
                zen.forEach {
                    ans.add(it + kou)
                }
            }
        }
    }
    ans.sortDescending()
    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>()

fun getHalfList(list:List<Item>, index:Int, subTotal:Int):Map<Int, List<Int>> {
    if(index > list.lastIndex) {
        val ret = mutableMapOf<Int, List<Int>>()
        ret[subTotal] = listOf(0)
        return ret
    }
    val ans = mutableMapOf<Int, MutableList<Int>>()
    val ret1 = getHalfList(list, index + 1, subTotal + list[index].value)
    val ret2 = getHalfList(list, index + 1, subTotal)
    val key = 1 shl (itemList.lastIndex - list[index].index)
    ret1.forEach{
        val total = it.key
        if(!ans.containsKey(total)) {
            ans[total] = mutableListOf()
        }
        ans[total]!!.addAll(it.value.map { it + key })
    }
    ret2.forEach {
        val total = it.key
        if(!ans.containsKey(total)) {
            ans[total] = mutableListOf()
        }
        ans[total]!!.addAll(it.value)
    }
    return ans
}
0