結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー semisagisemisagi
提出日時 2020-07-17 22:18:50
言語 Swift
(5.10.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,496 bytes
コンパイル時間 1,952 ms
コンパイル使用メモリ 131,168 KB
実行使用メモリ 19,132 KB
最終ジャッジ日時 2023-08-20 16:16:34
合計ジャッジ時間 6,987 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
10,560 KB
testcase_01 AC 4 ms
9,712 KB
testcase_02 AC 4 ms
9,664 KB
testcase_03 AC 93 ms
17,912 KB
testcase_04 AC 107 ms
18,700 KB
testcase_05 AC 94 ms
17,724 KB
testcase_06 AC 88 ms
17,216 KB
testcase_07 AC 110 ms
18,720 KB
testcase_08 AC 10 ms
10,224 KB
testcase_09 AC 63 ms
14,980 KB
testcase_10 AC 108 ms
18,996 KB
testcase_11 AC 5 ms
9,744 KB
testcase_12 AC 111 ms
18,896 KB
testcase_13 AC 111 ms
18,996 KB
testcase_14 AC 110 ms
19,132 KB
testcase_15 AC 4 ms
9,732 KB
testcase_16 AC 3 ms
9,696 KB
testcase_17 AC 3 ms
9,764 KB
testcase_18 AC 3 ms
9,768 KB
testcase_19 AC 4 ms
9,728 KB
testcase_20 AC 4 ms
9,716 KB
testcase_21 AC 3 ms
9,640 KB
testcase_22 AC 4 ms
9,752 KB
testcase_23 AC 12 ms
10,456 KB
testcase_24 AC 41 ms
12,976 KB
testcase_25 AC 82 ms
16,884 KB
testcase_26 AC 23 ms
12,072 KB
testcase_27 AC 50 ms
13,496 KB
testcase_28 AC 67 ms
14,800 KB
testcase_29 AC 88 ms
17,256 KB
testcase_30 AC 110 ms
18,396 KB
testcase_31 AC 30 ms
12,436 KB
testcase_32 AC 20 ms
11,408 KB
testcase_33 AC 94 ms
17,832 KB
testcase_34 AC 4 ms
9,740 KB
testcase_35 AC 4 ms
9,704 KB
testcase_36 AC 4 ms
9,684 KB
testcase_37 AC 3 ms
9,664 KB
testcase_38 AC 4 ms
9,728 KB
testcase_39 AC 3 ms
9,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

struct Scanner {
    private var stack = [String]()
    private var index = 0
    
    mutating func peek() -> String {
        if stack.count == index {
            stack += readLine()!.split(separator: " ").map{ String($0) }
        }
        return stack[index]
    }
    
    mutating func next() -> String {
        let value = peek()
        index += 1
        return value
    }
    
    mutating func nextInt() -> Int {
        return Int(next())!
    }
    
    mutating func nextDouble() -> Double {
        return Double(next())!
    }
}

var scanner = Scanner()
let N = scanner.nextInt()

let A = (0 ..< N).map { _ in scanner.nextInt() - 1 }
let B = (0 ..< N).map { _ in scanner.nextInt() - 1 }

var I = [Int](repeating: 0, count: N)
for i in A.indices {
    I[A[i]] = i
}

struct Counter {
    static let size = 500
    var this = [Int](repeating: 0, count: size * size)
    var these = [Int](repeating: 0, count: size)

    mutating func increment(_ index: Int) {
        this[index] += 1
        these[index / Self.size] += 1
    }
    
    func count(le: Int) -> Int {
        var value = 0
        var k = le
        while k % Self.size != 0 {
            value += this[k]
            k += 1
        }
        k /= Self.size
        while k < Self.size {
            value += these[k]
            k += 1
        }
        return value
    }
}

var counter = Counter()

var answer = 0
for x in B {
    answer += counter.count(le: I[x])
    counter.increment(I[x])
}
print(answer)
0