結果

問題 No.2089 置換の符号
ユーザー 👑 箱
提出日時 2022-09-30 21:26:01
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 434 ms / 2,000 ms
コード長 1,833 bytes
コンパイル時間 16,883 ms
コンパイル使用メモリ 425,380 KB
実行使用メモリ 60,232 KB
最終ジャッジ日時 2023-08-24 14:37:55
合計ジャッジ時間 26,007 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 303 ms
58,056 KB
testcase_01 AC 299 ms
57,908 KB
testcase_02 AC 302 ms
58,332 KB
testcase_03 AC 301 ms
56,772 KB
testcase_04 AC 300 ms
56,348 KB
testcase_05 AC 300 ms
58,004 KB
testcase_06 AC 300 ms
58,020 KB
testcase_07 AC 301 ms
56,208 KB
testcase_08 AC 300 ms
58,208 KB
testcase_09 AC 298 ms
58,160 KB
testcase_10 AC 299 ms
56,676 KB
testcase_11 AC 299 ms
56,780 KB
testcase_12 AC 298 ms
56,388 KB
testcase_13 AC 299 ms
56,276 KB
testcase_14 AC 312 ms
56,280 KB
testcase_15 AC 299 ms
58,240 KB
testcase_16 AC 302 ms
58,152 KB
testcase_17 AC 302 ms
56,648 KB
testcase_18 AC 303 ms
56,256 KB
testcase_19 AC 305 ms
56,268 KB
testcase_20 AC 305 ms
56,452 KB
testcase_21 AC 307 ms
58,400 KB
testcase_22 AC 306 ms
60,232 KB
testcase_23 AC 314 ms
58,288 KB
testcase_24 AC 314 ms
56,200 KB
testcase_25 AC 321 ms
57,572 KB
testcase_26 AC 317 ms
58,952 KB
testcase_27 AC 332 ms
58,908 KB
testcase_28 AC 418 ms
57,776 KB
testcase_29 AC 426 ms
59,476 KB
testcase_30 AC 430 ms
57,400 KB
testcase_31 AC 433 ms
57,516 KB
testcase_32 AC 434 ms
57,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val a = Array(n) { nextInt() }
    val inv = inversionNumber(a)
    println(if (inv % 2 == 0L) 1 else -1)
}

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