結果

問題 No.1209 XOR Into You
ユーザー mkawa2mkawa2
提出日時 2021-07-08 10:47:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 464 ms / 2,000 ms
コード長 1,593 bytes
コンパイル時間 129 ms
コンパイル使用メモリ 10,760 KB
実行使用メモリ 47,004 KB
最終ジャッジ日時 2023-09-14 04:45:30
合計ジャッジ時間 18,837 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,752 KB
testcase_01 AC 16 ms
8,744 KB
testcase_02 AC 20 ms
8,800 KB
testcase_03 AC 19 ms
8,624 KB
testcase_04 AC 69 ms
23,824 KB
testcase_05 AC 63 ms
24,040 KB
testcase_06 AC 115 ms
26,428 KB
testcase_07 AC 395 ms
38,880 KB
testcase_08 AC 373 ms
35,964 KB
testcase_09 AC 365 ms
35,904 KB
testcase_10 AC 292 ms
31,708 KB
testcase_11 AC 365 ms
35,900 KB
testcase_12 AC 335 ms
33,944 KB
testcase_13 AC 384 ms
37,308 KB
testcase_14 AC 329 ms
33,140 KB
testcase_15 AC 351 ms
34,632 KB
testcase_16 AC 352 ms
34,196 KB
testcase_17 AC 320 ms
33,084 KB
testcase_18 AC 464 ms
45,244 KB
testcase_19 AC 341 ms
34,064 KB
testcase_20 AC 372 ms
37,388 KB
testcase_21 AC 274 ms
31,216 KB
testcase_22 AC 400 ms
46,924 KB
testcase_23 AC 397 ms
46,876 KB
testcase_24 AC 398 ms
47,004 KB
testcase_25 AC 444 ms
46,720 KB
testcase_26 AC 442 ms
46,780 KB
testcase_27 AC 459 ms
46,668 KB
testcase_28 AC 447 ms
46,816 KB
testcase_29 AC 450 ms
46,664 KB
testcase_30 AC 445 ms
46,844 KB
testcase_31 AC 373 ms
28,524 KB
testcase_32 AC 378 ms
28,596 KB
testcase_33 AC 375 ms
28,644 KB
testcase_34 AC 381 ms
28,576 KB
testcase_35 AC 385 ms
28,636 KB
testcase_36 AC 403 ms
28,536 KB
testcase_37 AC 343 ms
30,520 KB
testcase_38 AC 364 ms
38,572 KB
testcase_39 AC 332 ms
35,428 KB
testcase_40 AC 407 ms
46,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = 10**16
# md = 998244353
md = 10**9+7

# 内部で1-indexedに変えるので入力は0-indexedでよい
class BitSum:
    def __init__(self, n):
        self.n = n+1
        self.table = [0]*self.n

    def add(self, i, x):
        i += 1
        while i < self.n:
            self.table[i] += x
            i += i & -i

    # [0,i]の和
    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self.table[i]
            i -= i & -i
        return res

from collections import defaultdict

n = II()
aa = LI()
bb = LI()

if aa[0] != bb[0] or aa[-1] != bb[-1]:
    print(-1)
    exit()

dd = [a0 ^ a1 for a0, a1 in zip(aa, aa[1:])]
ff = [a0 ^ a1 for a0, a1 in zip(bb, bb[1:])]

if sorted(dd) != sorted(ff):
    print(-1)
    exit()

dtoi = defaultdict(list)
for i, d in enumerate(ff):
    dtoi[d].append(i)

for d in dtoi: dtoi[d].reverse()

ii = [dtoi[d].pop() for d in dd]
bit = BitSum(n)
ans = 0
for j, i in enumerate(ii):
    ans += j-bit.sum(i)
    bit.add(i, 1)

print(ans)
0