結果

問題 No.297 カードの数式
ユーザー rutilicusrutilicus
提出日時 2020-10-07 21:49:22
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 329 ms / 1,000 ms
コード長 1,905 bytes
コンパイル時間 15,014 ms
コンパイル使用メモリ 432,324 KB
実行使用メモリ 57,248 KB
最終ジャッジ日時 2023-09-27 10:02:33
合計ジャッジ時間 24,607 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 326 ms
56,888 KB
testcase_01 AC 304 ms
52,804 KB
testcase_02 AC 307 ms
53,056 KB
testcase_03 AC 324 ms
57,052 KB
testcase_04 AC 299 ms
52,964 KB
testcase_05 AC 326 ms
57,248 KB
testcase_06 AC 321 ms
56,920 KB
testcase_07 AC 301 ms
52,992 KB
testcase_08 AC 298 ms
52,984 KB
testcase_09 AC 301 ms
52,868 KB
testcase_10 AC 302 ms
52,900 KB
testcase_11 AC 302 ms
52,760 KB
testcase_12 AC 310 ms
52,884 KB
testcase_13 AC 300 ms
52,796 KB
testcase_14 AC 301 ms
53,076 KB
testcase_15 AC 306 ms
52,808 KB
testcase_16 AC 298 ms
52,832 KB
testcase_17 AC 299 ms
52,980 KB
testcase_18 AC 300 ms
52,936 KB
testcase_19 AC 323 ms
56,936 KB
testcase_20 AC 316 ms
56,820 KB
testcase_21 AC 299 ms
57,004 KB
testcase_22 AC 286 ms
53,244 KB
testcase_23 AC 323 ms
57,228 KB
testcase_24 AC 329 ms
56,828 KB
testcase_25 AC 325 ms
56,824 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:75:13: warning: 'appendln(String?): kotlin.text.StringBuilder /* = java.lang.StringBuilder */' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
    builder.appendln("$max $min")
            ^

ソースコード

diff #

fun main() {
    val builder = StringBuilder()

    val n = readInputLine().toInt()

    var plusCnt = 0
    var minusCnt = 0
    val numCnt = IntArray(10)

    readInputLine().split(" ").forEach {
        when(it) {
            "+" -> plusCnt++
            "-" -> minusCnt++
            else -> numCnt[it.toInt()]++
        }
    }

    val digitCnt = n - plusCnt - minusCnt

    val allTermList = mutableListOf<Long>()

    var maxTermTmp = 0L
    var maxTermLen = 0

    val numCntTmp = numCnt.copyOf()

    for (i in 9 downTo 0) {
        while (numCntTmp[i] != 0) {
            if (maxTermLen < digitCnt - plusCnt - minusCnt) {
                maxTermTmp = maxTermTmp * 10L + i.toLong()
                maxTermLen++
                if (maxTermLen == digitCnt - plusCnt - minusCnt) {
                    allTermList.add(maxTermTmp)
                }
            } else {
                allTermList.add(i.toLong())
            }
            numCntTmp[i]--
        }
    }

    allTermList.sortDescending()

    var max = 0L
    for ((i, t) in allTermList.withIndex()) {
        if (i <= plusCnt) {
            max += t
        } else {
            max -= t
        }
    }

    var min = 0L
    if (minusCnt != 0) {
        for ((i, t) in allTermList.withIndex()) {
            if (i < minusCnt) {
                min -= t
            } else {
                min += t
            }
        }
    } else {
        val allTerm = LongArray(plusCnt + 1)
        var currentIndex = 0
        for (i in 0..9) {
            while (numCnt[i] != 0) {
                allTerm[currentIndex] = allTerm[currentIndex] * 10L + i.toLong()
                currentIndex = (currentIndex + 1) % (plusCnt + 1)
                numCnt[i]--
            }
        }
        min = allTerm.sum()
    }

    builder.appendln("$max $min")

    print(builder.toString())
}

fun readInputLine(): String {
    return readLine()!!
}
0