結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー 👑 箱
提出日時 2020-09-20 04:33:22
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 804 ms / 2,000 ms
コード長 1,998 bytes
コンパイル時間 14,582 ms
コンパイル使用メモリ 422,400 KB
実行使用メモリ 70,184 KB
最終ジャッジ日時 2023-09-06 10:01:15
合計ジャッジ時間 39,119 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 440 ms
58,176 KB
testcase_01 AC 285 ms
55,876 KB
testcase_02 AC 294 ms
55,880 KB
testcase_03 AC 770 ms
62,704 KB
testcase_04 AC 788 ms
69,112 KB
testcase_05 AC 730 ms
62,752 KB
testcase_06 AC 730 ms
64,884 KB
testcase_07 AC 783 ms
69,092 KB
testcase_08 AC 397 ms
57,828 KB
testcase_09 AC 520 ms
58,024 KB
testcase_10 AC 628 ms
68,072 KB
testcase_11 AC 285 ms
55,828 KB
testcase_12 AC 802 ms
69,104 KB
testcase_13 AC 804 ms
68,608 KB
testcase_14 AC 775 ms
70,184 KB
testcase_15 AC 285 ms
55,864 KB
testcase_16 AC 292 ms
55,828 KB
testcase_17 AC 291 ms
55,832 KB
testcase_18 AC 281 ms
56,128 KB
testcase_19 AC 295 ms
55,860 KB
testcase_20 AC 306 ms
56,424 KB
testcase_21 AC 294 ms
56,092 KB
testcase_22 AC 291 ms
56,132 KB
testcase_23 AC 427 ms
57,880 KB
testcase_24 AC 561 ms
60,316 KB
testcase_25 AC 741 ms
66,380 KB
testcase_26 AC 501 ms
58,904 KB
testcase_27 AC 573 ms
58,708 KB
testcase_28 AC 645 ms
60,932 KB
testcase_29 AC 726 ms
62,468 KB
testcase_30 AC 773 ms
69,264 KB
testcase_31 AC 527 ms
58,936 KB
testcase_32 AC 487 ms
58,576 KB
testcase_33 AC 598 ms
62,840 KB
testcase_34 AC 288 ms
56,152 KB
testcase_35 AC 285 ms
56,264 KB
testcase_36 AC 288 ms
55,872 KB
testcase_37 AC 288 ms
55,808 KB
testcase_38 AC 285 ms
56,104 KB
testcase_39 AC 293 ms
55,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*

fun compress(a: Array<Int>): Array<Int> {
    val n = a.count()
    val b = a.sorted().distinct().toTypedArray()
    val c = Array(n) { 0 }
    for (i in 0 until n) {
        c[i] = Arrays.binarySearch(b, a[i])
    }
    return c
}

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(l: Int, r: Int): Long {
        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.count()
    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 PrintWriter.solve(sc: FastScanner) {
    val n = sc.nextInt()
    val a = Array(n) { sc.nextInt() }
    val b = Array(n) { sc.nextInt() }
    val new = Array(n + 1) { 0 }
    for (i in 0 until n) {
        new[b[i]] = i + 1
    }
    for (i in 0 until n) {
        a[i] = new[a[i]]
    }
    println(inversionNumber(a))
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve(FastScanner(System.`in`))
    writer.flush()
}

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    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()
    fun ready() = br.ready()
}
0