結果

問題 No.90 品物の並び替え
ユーザー rutilicusrutilicus
提出日時 2020-09-02 21:31:08
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 491 ms / 5,000 ms
コード長 1,179 bytes
コンパイル時間 15,572 ms
コンパイル使用メモリ 440,204 KB
実行使用メモリ 52,424 KB
最終ジャッジ日時 2024-05-01 16:24:50
合計ジャッジ時間 17,749 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 291 ms
51,920 KB
testcase_01 AC 342 ms
52,200 KB
testcase_02 AC 285 ms
51,808 KB
testcase_03 AC 300 ms
51,764 KB
testcase_04 AC 315 ms
52,328 KB
testcase_05 AC 341 ms
52,372 KB
testcase_06 AC 331 ms
52,324 KB
testcase_07 AC 291 ms
51,892 KB
testcase_08 AC 285 ms
51,920 KB
testcase_09 AC 491 ms
52,424 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:29:13: warning: 'appendln(Int): 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(maxScore)
            ^

ソースコード

diff #

import kotlin.math.max

fun main() {
    val builder = StringBuilder()

    val (n, m) = readInputLine().split(" ").map { it.toInt() }

    val scores = Array(m) {
        val (i1, i2, s) = readInputLine().split(" ").map { it.toInt() }
        Triple(i1, i2, s)
    }

    val arr = IntArray(n) { it }

    var maxScore = 0

    do {
        var tmp = 0

        for (s in scores) {
            if (arr[s.first] < arr[s.second]) {
                tmp += s.third
            }
        }

        maxScore = max(maxScore, tmp)
    } while (nextPermutation(arr))

    builder.appendln(maxScore)

    print(builder.toString())
}

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

fun nextPermutation(arr: IntArray): Boolean {
    var i = arr.size - 1
    while (i > 0 && arr[i - 1] >= arr[i]) {
        i--
    }

    if (i <= 0) {
        return false
    }

    var j = arr.size - 1
    while (arr[j] <= arr[i - 1]) {
        j--
    }

    var temp = arr[i - 1];
    arr[i - 1] = arr[j];
    arr[j] = temp;

    j = arr.size - 1;
    while (i < j) {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        i++;
        j--;
    }

    return true;
}
0