結果

問題 No.2759 Take Pictures, Elements?
コンテスト
ユーザー 回転
提出日時 2025-11-14 14:56:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 755 bytes
コンパイル時間 458 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 78,764 KB
最終ジャッジ日時 2025-11-14 14:56:40
合計ジャッジ時間 3,925 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

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

place = defaultdict(list)
for i in range(N):
    place[A[i]].append(i)

INF = 10**18
now = defaultdict(lambda:INF)
now[0] = 0
for i in range(Q):
    next = defaultdict(lambda:INF)
    for j in now:
        left = bisect.bisect_right(place[B[i]],j) - 1
        if(left != -1):
            l = place[B[i]][left]
            next[l] = min(next[l], now[j] + abs(j - l))

        right = bisect.bisect_left(place[B[i]],j)
        if(right != len(place[B[i]])):
            r = place[B[i]][right]
            next[r] = min(next[r], now[j] + abs(j - r))

    now,next = next,now

print(min(now.values()))
0