結果

問題 No.2018 X-Y-X
ユーザー mkawa2mkawa2
提出日時 2022-07-23 00:00:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 1,326 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 87,116 KB
実行使用メモリ 87,580 KB
最終ジャッジ日時 2023-09-17 13:19:55
合計ジャッジ時間 6,813 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,644 KB
testcase_01 AC 76 ms
71,524 KB
testcase_02 AC 80 ms
71,312 KB
testcase_03 AC 78 ms
71,080 KB
testcase_04 AC 77 ms
71,188 KB
testcase_05 AC 76 ms
71,292 KB
testcase_06 AC 195 ms
81,940 KB
testcase_07 AC 250 ms
83,808 KB
testcase_08 AC 105 ms
81,096 KB
testcase_09 AC 103 ms
81,024 KB
testcase_10 AC 94 ms
79,216 KB
testcase_11 AC 100 ms
79,300 KB
testcase_12 AC 98 ms
79,820 KB
testcase_13 AC 186 ms
83,508 KB
testcase_14 AC 243 ms
84,680 KB
testcase_15 AC 244 ms
85,996 KB
testcase_16 AC 189 ms
81,316 KB
testcase_17 AC 243 ms
84,568 KB
testcase_18 AC 200 ms
82,280 KB
testcase_19 AC 156 ms
81,260 KB
testcase_20 AC 254 ms
87,580 KB
testcase_21 AC 175 ms
81,732 KB
testcase_22 AC 223 ms
82,104 KB
testcase_23 AC 76 ms
71,524 KB
testcase_24 AC 222 ms
85,916 KB
testcase_25 AC 262 ms
87,380 KB
testcase_26 AC 236 ms
81,404 KB
testcase_27 AC 170 ms
79,784 KB
testcase_28 AC 77 ms
71,184 KB
testcase_29 AC 95 ms
78,028 KB
testcase_30 AC 76 ms
71,572 KB
testcase_31 AC 218 ms
83,656 KB
testcase_32 AC 236 ms
84,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
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 = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

from heapq import *

n = II()
s = SI()
t = SI()

if s[0] != t[0] or s[-1] != t[-1]:
    print(-1)
    exit()

aa = [(s[i] != s[i+1])*1 for i in range(n-1)]
bb = [(t[i] != t[i+1])*1 for i in range(n-1)]
# print(aa)
# print(bb)

hp = []
for i in range(n-2):
    if aa[i] == aa[i+1]: heappush(hp, i+1)

ans = 0
for i in range(n-1):
    if aa[i] == bb[i]: continue
    while 1:
        if not hp:
            print(-1)
            exit()
        j = heappop(hp)
        if j > i and aa[j-1] == aa[j]: break
    aa[j] ^= 1
    if j+1 < n-1 and aa[j] == aa[j+1]: heappush(hp, j+1)
    ans += j-i

print(ans)
0