Skip to content

121 周赛

程序调用

这道题真的是完完全全的 coding 题,特别适合练习 coding 能力。

py
n, m, k = list(map(int, input().strip().split()))
all_process_id = list(map(int, input().strip().split()))
call_order = list(map(int, input().strip().split()))

def cost(index):
    return index // k + 1

def main():
    if n < k:
        print(m)
        return

    total_cost = 0
    get_index_by_id = {}
    get_id_by_index = {}

    for index, id in enumerate(all_process_id):
        get_index_by_id[id] = index
        get_id_by_index[index] = id

    for id in call_order:
        total_cost += cost(get_index_by_id[id])

        cur_index = get_index_by_id[id]
        if get_index_by_id[id] != 0:
            # old order
            cur_id = id
            pre_index = cur_index - 1
            pre_id = get_id_by_index[pre_index]

            # change order
            cur_index -= 1
            pre_index += 1

            # update mapping
            get_index_by_id[cur_id] = cur_index
            get_id_by_index[cur_index] = cur_id
            get_index_by_id[pre_id] = pre_index
            get_id_by_index[pre_index] = pre_id

    print(total_cost)

main()