結果

問題 No.2759 Take Pictures, Elements?
ユーザー NP
提出日時 2024-05-17 21:51:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 874 bytes
コンパイル時間 323 ms
コンパイル使用メモリ 82,584 KB
実行使用メモリ 66,020 KB
最終ジャッジ日時 2025-06-20 13:17:14
合計ジャッジ時間 2,207 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
input = sys.stdin.read

def main():
    data = input().split()
    N = int(data[0])
    Q = int(data[1])
    A = list(map(int, data[2:N+2]))
    B = list(map(int, data[N+2:]))
    value_to_indices = defaultdict(list)
    for index, value in enumerate(A, start=1):
        value_to_indices[value].append(index)
    
    current_position = 1
    total_cost = 0
    
    for target in B:
        possible_indices = value_to_indices[target]
        best_cost = float('inf')
        best_index = -1
        
        for index in possible_indices:
            cost = abs(current_position - index)
            if cost < best_cost:
                best_cost = cost
                best_index = index
        total_cost += best_cost
        current_position = best_index
    
    print(total_cost)

if __name__ == "__main__":
    main()
0