CF1855A Dalton the Teacher

题目描述

# Dalton the Teacher Dalton是一位老师,他有 $n$ 名学生,每名学生的编号为 $1\sim n$ ,教室里有 $n$ 把椅子,椅子的编号也是 $1\sim n$ 。一开始学生 $i$ 坐在凳子 $p_i$ 上,可以保证 $p_i$ 这个数不会出现多次,且 $p_i \le n$ 。 如果一个学生的编号与他(她)的椅子编号不相同,那么学生就会高兴。为了让所有学生都开心,Dalton可以将两个不同的学生交换椅子的编号,文让所有学生开心至少需要多少次交换?可以证明,在这个问题的约束下,用有限的动作让所有学生都感到高兴是可能的。

输入格式

第一行一个 $t$ ( $ 1 \le t \le 1000 $ ),表示测试用例的数量。 对于每个测试用例,第一行一个 $n$ ($2\le n\le 10^5$) 表示学生人数。 第二行一共 $n$ 各种整数 $ p_1, p_2, \ldots, p_n $ ( $ 1 \le p_i \le n $)表示学生 $i$ 的初始椅子编号。 保证所有测试用例的$n$总额不超过$10^5$。

输出格式

对于每个测试用例,输出所需的最小移动次数。

说明/提示

In the first test case, both students are already happy, so Dalton can perform $ 0 $ moves. In the second test case, Dalton can swap the chairs of students $ 1 $ and $ 2 $ to get the array $ [2, 1, 3] $ . Then he can swap chairs of students $ 2 $ and $ 3 $ to get the array $ [2, 3, 1] $ . At this point all the students are happy, and he performed $ 2 $ moves. It is impossible to perform the task with fewer moves. In the third test case, by swapping the chairs of students $ 1 $ and $ 2 $ and then swapping the chairs of students $ 4 $ and $ 5 $ , Dalton gets the array $ [2, 1, 5, 3, 4] $ in $ 2 $ moves.