| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 20 초 (추가 시간 없음) | 1024 MB | 11 | 8 | 6 | 66.667% |
A permutation cycle in a permutation $C$ is a sequence of integers $(a_1, a_2, \dots, a_k)$ such that the following hold:
A permutation cycle of length $k$ is called a $k$-cycle.
For example, the permutation $C = [4, 2, 1, 3]$ has two cycles: the 3ドル$-cycle $(4, 3, 1),ドル and the 1ドル$-cycle $(2)$. $(4, 3, 1)$ is a cycle because $C[4] = 3,ドル $C[3] = 1,ドル and $C[1] = 4$.
Grace loves permutation cycles, so Charles decides to design an $\mathbf{N}$-level game to challenge her.
At the start of the game, the player is given an $\mathbf{N}$-length permutation $\mathbf{P}$ of integers from 1ドル$ through $\mathbf{N}$. The levels in the game are numbered from 1ドル$ to $\mathbf{N}$. At each level, the player starts with the given permutation, and is allowed to make modifications to it by swapping any two elements in it (multiple swaps allowed). To clear the $k$-th level in the game, the player is required to find the minimum number of swaps using which a $k$-cycle can be created in the permutation. The player can progress to the $(k+1)$-th level only after clearing the $k$-th level.
Grace finds the game a bit challenging, but wants to win at any cost. She needs your help! Formally, for each level $k,ドル you need to find the minimum number of swaps using which a $k$-cycle can be created in the permutation.
The first line of the input gives the number of test cases, $\mathbf{T}$. $\mathbf{T}$ test cases follow.
The first line of each test case contains an integer $\mathbf{N}$: the length of the permutation.
The next line contains $\mathbf{N}$ integers $\mathbf{P_1},ドル $\mathbf{P_2},ドル $\dots,ドル $\mathbf{P_N},ドル where the $i$-th integer represents the $i$-th element in the permutation $\mathbf{P}$.
For each test case, output one line containing Case #x: S1, S2, ..., SN, where $x$ is the test case number (starting from 1), and $S_i$ is the solution for the $i$-th level in the game, that is, the minimum number of swaps needed to create an $i$-cycle in the permutation.
2 3 1 2 3 4 4 2 1 3
Case #1: 0 1 2 Case #2: 0 1 0 1
In Sample Case #1, there are three 1ドル$-cycles in the given permutation. So, the first level can be cleared with zero swaps. To clear the second level, we can swap the first two elements to get the permutation $[2, 1, 3],ドル which contains the 2ドル$-cycle $(2, 1)$. To clear the third level, we can swap the first two elements, followed by the second and third elements to get the permutation $[2, 3, 1],ドル which contains the 3ドル$-cycle $(2, 3, 1)$.
In Sample Case #2, as explained earlier, the permutation has the 1ドル$-cycle $(2)$. So, zero swaps are needed to clear the first level. To clear the second level, we can swap the last two elements to get the permutation $[4, 2, 3, 1],ドル which contains the 2ドル$-cycle $(4, 1)$. Since the permutation also has the 3ドル$-cycle $(4, 3, 1),ドル the third level can also be cleared using zero swaps. To clear the fourth level, we can swap the second and the fourth elements to get the permutation $[4, 3, 1, 2],ドル which contains the 4ドル$-cycle $(4, 2, 3, 1)$.
Contest > Google > Kick Start > Google Kick Start 2022 > Round H D번