목차

Swaps

ps
링크acmicpc.net/…
출처BOJ
문제 번호13214
문제명Swaps
레벨골드 4
분류

순열 사이클 분할

시간복잡도O(n)
인풋사이즈n<=1,000,000
사용한 언어Python 3.13
제출기록107604KB / 1028ms
최고기록1028ms
해결날짜2025/11/13

풀이

코드

"""Solution code for "BOJ 13214. Swaps".

- Problem link: https://www.acmicpc.net/problem/13214
- Solution link: http://www.teferi.net/ps/problems/boj/13214

Tags: [permutation cycle]
"""

import array


def count_permutation_cycles(perm):
    visited = [False] * len(perm)
    ret = 0
    for i in range(len(perm)):
        if visited[i]:
            continue
        cur = i
        ret += 1
        while (cur := perm[cur]) != i:
            visited[cur] = True
    return ret


def main():
    N = int(input())
    A = array.array('i', (int(x) - 1 for x in input().split()))
    B = array.array('i', (int(x) - 1 for x in input().split()))

    pos = array.array('i', [-1] * N)
    for i, b_i in enumerate(B):
        pos[b_i] = i
    perm = array.array('i', (pos[a_i] for a_i in A))
    answer = N - count_permutation_cycles(perm)

    print(answer)


if __name__ == '__main__':
    main()