. https://acs.jxnu.edu.cn/contest/24/board/challenge/C
Sorting by Swapping
描述:
Given a permutation of numbers from 1 to n, we can always get the sequence 1, 2, 3, ..., n by swapping pairs of numbers. For example, if the initial sequence is 2, 3, 5, 4, 1, we can sort them in the following way: (给定一个从1到n的排序,我们可以通过交换得到顺序排序。例如,如果我们的原始排序是2, 3, 5, 4, 1,我们可以将交换的过程列出如下:)
2 3 5 4 1
1 3 5 4 2
1 3 2 4 5
1 2 3 4 5
Here three swaps have been used. The problem is, given a specific permutation, how many swaps we needs to take at least.
(这里进行了三次交换。问题就是 给定一个特定的序列,得到我们需要交换的最少次数。)
输入:
The first line contains a single integer t (1 <= t <= 20) that indicates the number of test cases. Then follow the t cases. Each case contains two lines. The first line contains the integer n (1 <= n <= 10000), and the second line gives the initial permutation.
(第一行包含一共整数t 其表示测试的例子数。紧接着的是t个例子。每个例子有两行。第一行有一个整数n,第二行给定原始排序。)
输出:
For each test case, the output will be only one integer, which is the least number of swaps needed to get the sequence 1, 2, 3, ..., n from the initial permutation.
(对于每个例子,输出一共整数,其是将原始排序转化成顺序排序的最小排序次数。)