#P16900. [GKS 2022 #H] Level Design

    ID: 19228 远端评测题 10000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP并查集单调队列2022背包 DP置换Google Kick Start

[GKS 2022 #H] Level Design

Problem Description

A permutation cycle in a permutation CC is a sequence of integers (a1,a2,,ak)(a_1, a_2, \ldots, a_k) such that the following hold:

  • aiCa_i \in C for all ii, and are distinct.
  • For each i{1,2,,k1}i \in \{1, 2, \ldots, k-1\}: C[ai]=ai+1C[a_i] = a_{i+1}, and C[ak]=a1C[a_k] = a_1.

A permutation cycle of length kk is called a kk-cycle.

For example, the permutation C=[4,2,1,3]C = [4, 2, 1, 3] has two cycles: the 33-cycle (4,3,1)(4, 3, 1), and the 11-cycle (2)(2). (4,3,1)(4, 3, 1) is a cycle because C[4]=3C[4] = 3, C[3]=1C[3] = 1, and C[1]=4C[1] = 4.

:::align{center} :::

Grace loves permutation cycles, so Charles decides to design an NN-level game to challenge her.

At the start of the game, the player is given an NN-length permutation PP of integers from 11 through NN. The levels in the game are numbered from 11 to NN. 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 kk-th level in the game, the player is required to find the minimum number of swaps using which a kk-cycle can be created in the permutation. The player can progress to the (k+1)(k+1)-th level only after clearing the kk-th level.

Grace finds the game a bit challenging, but wants to win at any cost. She needs your help! Formally, for each level kk, you need to find the minimum number of swaps using which a kk-cycle can be created in the permutation.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains an integer NN: the length of the permutation.

The next line contains NN integers P1,P2,,PNP_1, P_2, \ldots, P_N, where the ii-th integer represents the ii-th element in the permutation PP.

Output Format

For each test case, output one line containing Case #xx: S1,S2,,SNS_1, S_2, \ldots, S_N, where xx is the test case number (starting from 11), and SiS_i is the solution for the ii-th level in the game, that is, the minimum number of swaps needed to create an ii-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

Hint

In Sample Case #11, there are three 11-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][2, 1, 3], which contains the 22-cycle (2,1)(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][2, 3, 1], which contains the 33-cycle (2,3,1)(2, 3, 1).

In Sample Case #22, as explained earlier, the permutation has the 11-cycle (2)(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][4, 2, 3, 1], which contains the 22-cycle (4,1)(4, 1). Since the permutation also has the 33-cycle (4,3,1)(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][4, 3, 1, 2], which contains the 44-cycle (4,2,3,1))(4, 2, 3, 1)).

Limits

1T100.1 \le T \le 100.

1PiN1 \le P_i \le N, for all ii.

All PiP_i are distinct.

Test Set 11

1N103.1 \le N \le 10^3.

Test Set 22

1N105.1 \le N \le 10^5.