Created
December 5, 2023 12:09
-
-
Save sawaYch/6ad0c1d84fc86795499af1d91fe364ac to your computer and use it in GitHub Desktop.
B141:【異世界コラボ問題】ガーベラ・コレクション
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# 自分の得意な言語で | |
# Let's チャレンジ!! | |
def min_adjacent_swap(arr): | |
swap_times = 0 | |
n = len(arr) / 2 | |
iter = 0 | |
while iter < len(arr) - 1: | |
# if current not ideal | |
if arr[iter] != iter % n: | |
# found closest ideal | |
search_idx = iter | |
while search_idx < len(arr) - 1: | |
if arr[search_idx] == iter % n: | |
break | |
search_idx += 1 | |
# bubble swap | |
i = search_idx | |
while i > iter: | |
arr[i], arr[i-1] = arr[i-1], arr[i] | |
swap_times += 1 | |
i -= 1 | |
iter += 1 | |
return swap_times | |
input_line = int(input()) | |
i = 0 | |
data = [] | |
while i < 2*input_line: | |
data.append(int(input())) | |
i+=1 | |
normalize_input = [x-1 for x in data] | |
print(min_adjacent_swap(normalize_input)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment