結果

問題 No.2759 Take Pictures, Elements?
ユーザー ntuda
提出日時 2024-05-18 13:08:51
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 781 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 82,644 KB
実行使用メモリ 69,648 KB
最終ジャッジ日時 2025-06-20 13:19:53
合計ジャッジ時間 2,475 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

N, Q = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
from collections import defaultdict

dic = defaultdict(list)
for i, a in enumerate(A):
    dic[a].append(i)
INF = 10 ** 6

from atcoder.segtree import SegTree
base = [INF] * N
ST1a = SegTree(min, INF, base)
ST2a = SegTree(min, INF, base)
ST1a.set(0, 0)
ST2a.set(0, 0)

for b in B:
    ST1b = SegTree(min, INF, base)
    ST2b = SegTree(min, INF, base)
    BB = [-1] * N
    for c in dic[b]:
        a = ST1a.prod(c, N) - c
        a = min(a, ST2a.prod(0, c) + c)
        #BB[c] = a
        ST1b.set(c, a + c)
        ST2b.set(c, a - c)
    #print(BB)
    ST1a = ST1b
    ST2a = ST2b

ans = INF
for i in range(N):
    ans = min(ans, ST1a.get(i) - i, ST2a.get(i) + i)
print(ans)
0