結果

問題 No.2382 Amidakuji M
ユーザー 👑 箱
提出日時 2023-02-18 17:16:17
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 595 ms / 2,000 ms
コード長 1,387 bytes
コンパイル時間 17,450 ms
コンパイル使用メモリ 446,888 KB
実行使用メモリ 86,788 KB
最終ジャッジ日時 2024-05-02 02:49:33
合計ジャッジ時間 22,669 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 280 ms
51,684 KB
testcase_01 AC 280 ms
51,592 KB
testcase_02 AC 278 ms
51,760 KB
testcase_03 AC 495 ms
66,620 KB
testcase_04 AC 544 ms
74,936 KB
testcase_05 AC 451 ms
57,768 KB
testcase_06 AC 495 ms
67,156 KB
testcase_07 AC 478 ms
63,252 KB
testcase_08 AC 339 ms
52,696 KB
testcase_09 AC 534 ms
72,352 KB
testcase_10 AC 571 ms
79,668 KB
testcase_11 AC 499 ms
63,992 KB
testcase_12 AC 595 ms
73,528 KB
testcase_13 AC 289 ms
51,664 KB
testcase_14 AC 292 ms
51,732 KB
testcase_15 AC 294 ms
51,704 KB
testcase_16 AC 287 ms
51,716 KB
testcase_17 AC 281 ms
51,580 KB
testcase_18 AC 269 ms
51,832 KB
testcase_19 AC 572 ms
86,564 KB
testcase_20 AC 573 ms
86,736 KB
testcase_21 AC 593 ms
86,788 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:2:10: warning: variable 'n' is never used
    val (n, m) = readLongs()
         ^

ソースコード

diff #

fun main() {
    val (n, m) = readLongs()
    val p = readInts().map { it - 1 }
    val inv = inversionNumber(p)
    if (m % 2 == 0L && inv % 2 != 0L) {
        println(-1)
    } else {
        val s = (inv + m - 1) / m * m
        val k = s - inv
        if (k % 2 == 0L) {
            println(s)
        } else {
            val t = s + m
            val k2 = t - inv
            if (k2 % 2 == 0L) {
                println(t)
            } else {
                throw Exception()
            }
        }
    }
}

fun readLongs() = readln().split(" ").map { it.toLong() }
fun readInts() = readln().split(" ").map { it.toInt() }

class BIT(private val n: Int) {
    private val data = LongArray(n) { 0 }

    fun add(i: Int, x: Long) {
        var p = i + 1
        while (p <= n) {
            data[p - 1] += x
            p += p and -p
        }
    }

    fun sum(range: IntRange): Long {
        val l = range.first
        val r = range.last + 1
        return sum(r) - sum(l)
    }

    private fun sum(i: Int): Long {
        var r = i
        var s = 0L
        while(r > 0) {
            s += data[r - 1]
            r -= r and -r
        }
        return s
    }
}

fun inversionNumber(a: List<Int>): Long {
    val n = a.size
    val bit = BIT(n)
    var ans = 0L
    for (i in 0 until n) {
        ans += i - bit.sum(0..a[i])
        bit.add(a[i], 1)
    }
    return ans
}
0