結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 50 ms
54,532 KB
testcase_03 AC 43 ms
54,588 KB
testcase_04 AC 42 ms
53,996 KB
testcase_05 AC 42 ms
54,792 KB
testcase_06 WA -
testcase_07 AC 42 ms
55,804 KB
testcase_08 AC 43 ms
54,504 KB
testcase_09 AC 49 ms
61,560 KB
testcase_10 AC 49 ms
62,556 KB
testcase_11 AC 49 ms
61,712 KB
testcase_12 AC 49 ms
62,596 KB
testcase_13 AC 48 ms
61,224 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 44 ms
54,904 KB
testcase_22 AC 42 ms
55,868 KB
testcase_23 AC 51 ms
54,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
import bisect
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
    
    def find_nearest_index(indices, current_position):
        pos = bisect.bisect_left(indices, current_position)
        if pos == 0:
            return indices[0]
        if pos == len(indices):
            return indices[-1]
        before = indices[pos - 1]
        after = indices[pos]
        if abs(before - current_position) <= abs(after - current_position):
            return before
        else:
            return after
    
    for target in B:
        possible_indices = value_to_indices[target]
        best_index = find_nearest_index(possible_indices, current_position)
        total_cost += abs(current_position - best_index)
        current_position = best_index
    
    print(total_cost)

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