結果

問題 No.733 分身並列コーディング
ユーザー kaji
提出日時 2018-09-22 02:15:01
言語 Kotlin
(2.1.0)
結果
TLE  
実行時間 -
コード長 1,385 bytes
コンパイル時間 13,648 ms
コンパイル使用メモリ 450,744 KB
実行使用メモリ 173,788 KB
最終ジャッジ日時 2024-11-20 16:46:07
合計ジャッジ時間 48,296 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39 WA * 6 TLE * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^

ソースコード

diff #

import java.util.*

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    
    val T = scanner.nextInt()
    val N = scanner.nextInt()

    val times = (1..N).map { scanner.nextInt() }.sortedDescending().toMutableList()
    val combinationsCache = mutableMapOf<Int, List<MutableList<Int>>>()
    var count = 0

    for (t in T downTo 1) {
        for (i in 1..t) {
            generateSequence {
                combinationsCache
                        .getOrPut(i) { times.combinations(i) }
                        .find { it.sum() == t }
            }.forEach { c ->
                c.forEach { times.remove(it) }
                combinationsCache.clear()
                ++count

                if (times.size == 0) {
                    println(count)
                    return
                }
            }
        }
    }
}

fun <T> List<T>.combinations(n: Int): List<MutableList<T>> {
    return when {
        n > size -> emptyList()
        n == 1 -> map { mutableListOf(it) }
        else -> {
            val last = size
            (0 until last).fold(emptyList()) { all, index -> 
                all + subList(index + 1, last)
                        .combinations(n - 1)
                        .map { 
                            it.add(0, get(index)) 
                            it 
                        } 
            }
        }
    }
}
0