結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー hirakuhiraku
提出日時 2021-05-24 21:34:28
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 986 ms / 2,000 ms
コード長 1,367 bytes
コンパイル時間 109 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 103,568 KB
最終ジャッジ日時 2023-08-03 08:14:41
合計ジャッジ時間 17,777 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
15,976 KB
testcase_01 AC 19 ms
8,532 KB
testcase_02 AC 18 ms
8,596 KB
testcase_03 AC 772 ms
88,012 KB
testcase_04 AC 936 ms
102,368 KB
testcase_05 AC 769 ms
87,688 KB
testcase_06 AC 689 ms
81,260 KB
testcase_07 AC 986 ms
101,900 KB
testcase_08 AC 47 ms
13,348 KB
testcase_09 AC 389 ms
62,644 KB
testcase_10 AC 736 ms
103,568 KB
testcase_11 AC 19 ms
8,508 KB
testcase_12 AC 950 ms
103,484 KB
testcase_13 AC 939 ms
103,512 KB
testcase_14 AC 941 ms
103,400 KB
testcase_15 AC 19 ms
8,556 KB
testcase_16 AC 19 ms
8,684 KB
testcase_17 AC 19 ms
8,664 KB
testcase_18 AC 20 ms
8,660 KB
testcase_19 AC 19 ms
8,644 KB
testcase_20 AC 19 ms
8,616 KB
testcase_21 AC 19 ms
8,572 KB
testcase_22 AC 20 ms
8,580 KB
testcase_23 AC 70 ms
16,244 KB
testcase_24 AC 281 ms
41,396 KB
testcase_25 AC 656 ms
77,560 KB
testcase_26 AC 135 ms
24,932 KB
testcase_27 AC 359 ms
50,500 KB
testcase_28 AC 513 ms
64,856 KB
testcase_29 AC 706 ms
81,960 KB
testcase_30 AC 903 ms
98,840 KB
testcase_31 AC 193 ms
31,472 KB
testcase_32 AC 123 ms
23,404 KB
testcase_33 AC 644 ms
89,124 KB
testcase_34 AC 19 ms
8,628 KB
testcase_35 AC 19 ms
8,528 KB
testcase_36 AC 19 ms
8,556 KB
testcase_37 AC 19 ms
8,640 KB
testcase_38 AC 20 ms
8,668 KB
testcase_39 AC 19 ms
8,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque, Counter

# 2つの数列 隣接swap 一致させる 要素に重複なし


# 転倒数
def mergeCount(A):
    cnt = 0
    n = len(A)
    if n > 1:
        A1 = A[:n >> 1]
        A2 = A[n >> 1:]
        cnt += mergeCount(A1)
        cnt += mergeCount(A2)
        i1 = 0
        i2 = 0
        for i in range(n):
            if i2 == len(A2):
                A[i] = A1[i1]
                i1 += 1
            elif i1 == len(A1):
                A[i] = A2[i2]
                i2 += 1
            elif A1[i1] <= A2[i2]:
                A[i] = A1[i1]
                i1 += 1
            else:
                A[i] = A2[i2]
                i2 += 1
                cnt += n//2 - i1
    return cnt


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

if Counter(A) != Counter(B):
    print(-1)
    exit()

# print(A)
# print(B)

# A[i] = B[s_i] となるような配列 S (s_0, s_1, ,,,)を考える
# A[i], B[i]で要素の値が等しいものが複数あるときは、i < j, s_i < s_jとなるように決める
# 決定したSに対して転倒数を求める

# Bの要素の値ごとの位置を調べる
pos = defaultdict(deque)
for itr, b in enumerate(B):
    pos[b].append(itr)

S = []
for a in A:
    p = pos[a].popleft()
    S.append(p)
# print(S)

print(mergeCount(S))
0