結果
| 問題 |
No.15 カタログショッピング
|
| コンテスト | |
| ユーザー |
バカらっく
|
| 提出日時 | 2019-09-01 05:54:13 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,072 bytes |
| コンパイル時間 | 16,610 ms |
| コンパイル使用メモリ | 455,524 KB |
| 実行使用メモリ | 143,124 KB |
| 最終ジャッジ日時 | 2024-11-27 06:20:48 |
| 合計ジャッジ時間 | 26,459 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 WA * 1 |
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
^
ソースコード
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
}
バカらっく