题目地址: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的数字排列,我们可以通过交换成对的数字得到一个序列1,2,3,...,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(1 <= t <= 20)表示测试样例的数量。随着t组样例。然后按照t个样例进行操作。每个样例包含两行。第一行包括一个整数n(1 <= n <= 10000),第二行给出初始序列。
输出:
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.
对于每组测试样例,输出只有一个整数。交换后最后的数字需要从初始的序列得到序列1,2,3,..., n。
样例输入:
2 3 1 2 3 5 2 3 5 4 1
复制
样例输出:
0 3