結果

問題 No.2759 Take Pictures, Elements?
コンテスト
ユーザー NP
提出日時 2024-05-17 21:51:14
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 874 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 611 ms
コンパイル使用メモリ 95,984 KB
実行使用メモリ 84,572 KB
最終ジャッジ日時 2026-07-12 13:41:07
合計ジャッジ時間 3,657 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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