问题 C: Sort4

内存限制:256 MB 时间限制:1 S
题面:Markdown 评测方式:文本比较 上传者:
提交:6 通过:3

题目描述

Given a permutation$\textstyle ^\dagger$ of length $\textstyle n$. In one operation, you can choose **at most** four elements from the permutation and swap their positions arbitrarily. What is the minimum number of operations required to sort the permutation in ascending order? $\textstyle ^\dagger$ A permutation of length $\textstyle n$ is an array consisting of $\textstyle n$ distinct integers from $\textstyle 1$ to $\textstyle n$ in arbitrary order. For example, $\textstyle [2,3,1,5,4]$ is a permutation, but $\textstyle [1,2,2]$ is not a permutation ($\textstyle 2$ appears twice in the array), and $\textstyle [1,3,4]$ is also not a permutation ($\textstyle n=3$ but there is $\textstyle 4$ in the array).

输入格式

Each test contains multiple test cases. The first line contains the number of test cases $\textstyle t$ ($\textstyle 1 \leq t \leq 10^5$). The description of the test cases follows. The first line of each test case contains an integer $\textstyle n$ ($\textstyle 1 \leq n \leq 10^6$), indicating the length of the permutation. The second line contains a permutation $\textstyle a_1, a_2, \ldots, a_n$. It is guaranteed that the sum of $\textstyle n$ across all test cases does not exceed $\textstyle 10^6$.

输出格式

For each test case, output a single integer, indicating the minimum number of operations required to sort the permutation.

输入样例 复制

4
4
4 2 1 3
6
4 2 5 1 3 6
8
5 4 6 3 1 8 2 7
10
1 2 9 10 3 6 8 4 5 7

输出样例 复制

1
1
3
2