結果

問題 No.2759 Take Pictures, Elements?
ユーザー Ekiben542Ekiben542
提出日時 2024-05-17 21:51:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 874 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 65,936 KB
最終ジャッジ日時 2024-05-17 21:51:17
合計ジャッジ時間 2,104 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 41 ms
54,236 KB
testcase_03 AC 41 ms
55,300 KB
testcase_04 AC 40 ms
54,368 KB
testcase_05 AC 41 ms
55,112 KB
testcase_06 WA -
testcase_07 AC 40 ms
54,304 KB
testcase_08 AC 39 ms
54,100 KB
testcase_09 AC 47 ms
61,204 KB
testcase_10 AC 46 ms
62,712 KB
testcase_11 AC 46 ms
61,020 KB
testcase_12 AC 44 ms
62,284 KB
testcase_13 AC 45 ms
61,584 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 41 ms
54,312 KB
testcase_22 AC 40 ms
55,004 KB
testcase_23 AC 41 ms
53,940 KB
権限があれば一括ダウンロードができます

ソースコード

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