結果

問題 No.2382 Amidakuji M
ユーザー 👑 箱
提出日時 2023-01-12 19:04:59
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,122 ms / 2,000 ms
コード長 2,215 bytes
コンパイル時間 18,646 ms
コンパイル使用メモリ 424,404 KB
実行使用メモリ 80,408 KB
最終ジャッジ日時 2023-08-23 10:48:20
合計ジャッジ時間 30,133 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 306 ms
58,088 KB
testcase_01 AC 305 ms
58,332 KB
testcase_02 AC 300 ms
58,084 KB
testcase_03 AC 750 ms
64,896 KB
testcase_04 AC 939 ms
71,360 KB
testcase_05 AC 525 ms
58,980 KB
testcase_06 AC 745 ms
62,868 KB
testcase_07 AC 656 ms
63,236 KB
testcase_08 AC 357 ms
57,392 KB
testcase_09 AC 913 ms
70,876 KB
testcase_10 AC 1,028 ms
73,996 KB
testcase_11 AC 686 ms
65,248 KB
testcase_12 AC 906 ms
71,528 KB
testcase_13 AC 305 ms
58,144 KB
testcase_14 AC 304 ms
58,332 KB
testcase_15 AC 302 ms
56,480 KB
testcase_16 AC 309 ms
57,848 KB
testcase_17 AC 307 ms
57,868 KB
testcase_18 AC 307 ms
57,988 KB
testcase_19 AC 1,061 ms
80,408 KB
testcase_20 AC 1,122 ms
79,200 KB
testcase_21 AC 1,069 ms
78,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun PrintWriter.solve() {
    val n = nextInt()
    val m = nextLong()
    val p = Array(n) { nextInt() }
    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 compress(a: Array<Int>): Array<Int> {
    val b = a.sorted().distinct().toTypedArray()
    return a.map { b.binarySearch(it) }.toTypedArray()
}

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: Array<Int>): Long {
    val b = compress(a)
    val n = a.size
    val bit = BIT(n)
    var ans = 0L
    for (i in 0 until n) {
        ans += i - bit.sum(0..b[i])
        bit.add(b[i], 1)
    }
    return ans
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", abs(1L.shl(26)))
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0