Skip to content

Instantly share code, notes, and snippets.

@sawaYch
Created December 5, 2023 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sawaYch/6ad0c1d84fc86795499af1d91fe364ac to your computer and use it in GitHub Desktop.
Save sawaYch/6ad0c1d84fc86795499af1d91fe364ac to your computer and use it in GitHub Desktop.
B141:【異世界コラボ問題】ガーベラ・コレクション
# 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