結果
| 問題 | No.1115 二つの数列 / Two Sequences |
| コンテスト | |
| ユーザー |
semisagi
|
| 提出日時 | 2020-07-17 22:18:50 |
| 言語 | Swift (6.0.3) |
| 結果 |
AC
|
| 実行時間 | 143 ms / 2,000 ms |
| コード長 | 1,496 bytes |
| コンパイル時間 | 1,004 ms |
| コンパイル使用メモリ | 128,788 KB |
| 実行使用メモリ | 20,508 KB |
| 最終ジャッジ日時 | 2024-11-30 19:25:00 |
| 合計ジャッジ時間 | 5,113 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 35 |
ソースコード
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)
semisagi