https://www.acmicpc.net/problem/1091
import sys
from collections import defaultdict
input = sys.stdin.readline
N = int(input())
P = list(map(int, input().split()))
S = list(map(int, input().split()))
answer = defaultdict(set)
visited = set()
# 각 사람이 가져야하는 카드의 번호 중복 없이 저장
for i in range(N):
answer[P[i]].add(i)
cnt = 0
# 첫 카드 배열
cards = list(range(N))
while True:
first,second,third = set(cards[0:N:3]), set(cards[1:N:3]), set(cards[2:N:3])
# 카드가 다 제대로 나눠지면 끝 (섞인 카드와 가져야하는 카드가 같으면 cnt 출력)
if first == answer[0] and second == answer[1] and third == answer[2]:
break
# 한바퀴를 돌아서 P를 만족하지 않고 같은 조합이 나오면
# -1 출력
if tuple(cards) in visited:
cnt = -1
break
# 조합을 visited에 저장
visited.add(tuple(cards))
# S[i]에 맞게 카드 섞기
tmp = [0] * N
for i in range(N):
tmp[S[i]] = cards[i]
cards = tmp
cnt += 1
print(cnt)'코딩테스트 > 코딩테스트 문제풀이' 카테고리의 다른 글
| [Python] 프로그래머스 <N개의 최소공배수> (0) | 2026.05.05 |
|---|---|
| [Python] 백준 <1043-거짓말> (0) | 2025.08.28 |
| [Python] 백준 <1112-진법 변환> (0) | 2025.08.28 |
| [Python] 프로그래머스 <미로 탈출 명령어> (0) | 2025.08.20 |
| [Python] 프로그래머스 <풍선 터트리기> (0) | 2025.08.15 |