Lucky Permutation
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
题面翻译
题目描述
给定整数 和一个 的排列 。
你可以对排列 进行下列操作任意次:
- 选择整数 ,然后交换 的值。
你需要求出至少需要进行上述操作多少次才能使 恰有一个逆序对。
每个测试点包含 组数据。
输入格式
第一行输入一个整数 表示数据组数,接下来对于每组数据:
第一行输入一个整数 。
接下来输入一行 个整数 ,保证 是一个 的排列。
输出格式
对于每组数据:
输出一行一个整数表示使 恰有一个逆序对所需的最小操作次数。
可以证明一定存在操作方案使得 恰有一个逆序对。
题目描述
You are given a permutation of length .
In one operation, you can choose two indices and swap with .
Find the minimum number of operations needed to have exactly one inversion in the permutation.
A permutation is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array).
The number of inversions of a permutation is the number of pairs of indices such that and .
输入格式
The first line contains a single integer — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer ( ).
The second line of each test case contains integers ( ). It is guaranteed that is a permutation.
输出格式
For each test case output a single integer — the minimum number of operations needed to have exactly one inversion in the permutation. It can be proven that an answer always exists.
样例 #1
样例输入 #1
4
2
2 1
2
1 2
4
3 4 1 2
4
2 4 3 1
样例输出 #1
0
1
3
1
提示
In the first test case, the permutation already satisfies the condition.
In the second test case, you can perform the operation with , after that the permutation will be which has exactly one inversion.
In the third test case, it is not possible to satisfy the condition with less than operations. However, if we perform operations with being , , and in that order, the final permutation will be which has exactly one inversion.
In the fourth test case, you can perform the operation with , after that the permutation will be which has exactly one inversion.
【数据范围】
对于 50%的数据,满足 n≤5。
对于 80% 的数据,满足 n≤1000。
对于 100% 的数据,满足 1≤𝑛≤100000,1≤𝑡≤5。