結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー rutilicusrutilicus
提出日時 2020-12-19 21:33:50
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 721 ms / 2,000 ms
コード長 1,111 bytes
コンパイル時間 14,890 ms
コンパイル使用メモリ 435,732 KB
実行使用メモリ 86,852 KB
最終ジャッジ日時 2024-09-21 10:43:16
合計ジャッジ時間 35,219 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:18:13: warning: 'appendln(Long): 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(mergeCount(bArr))
            ^

ソースコード

diff #

fun main() {
    val builder = StringBuilder()

    val n = readInputLine().toInt()

    val num2Index = IntArray(n + 1)

    readInputLine().split(" ").mapIndexed { index, s ->
        num2Index[s.toInt()] = index
    }

    val bArr = readInputLine().split(" ").map { it.toInt() }.toIntArray()

    bArr.forEachIndexed { index, i ->
        bArr[index] = num2Index[i]
    }

    builder.appendln(mergeCount(bArr))

    print(builder.toString())
}

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

fun mergeCount(arr: IntArray): Long {
    val n = arr.size
    if (n <= 1L) {
        return 0L
    }
    val b = IntArray(n / 2) { arr[it] }
    val c = IntArray(arr.size - (n / 2)) { arr[it + n / 2] }

    var count = 0L
    count += mergeCount(b)
    count += mergeCount(c)
    var ai = 0
    var bi = 0
    var ci = 0
    while (ai < n) {
        if (bi < b.size && (ci == c.size || b[bi] <= c[ci])) {
            arr[ai] = b[bi]
            bi++
        } else {
            count += (n / 2 - bi).toLong()
            arr[ai] = c[ci]
            ci++
        }
        ai++
    }
    return count
}
0