欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1855A.Dalton the Teacher
Dalton the Teacher
Dalton is the teacher of a class with students, numbered from to . The classroom contains chairs, also numbered from to . Initially student is seated on chair . It is guaranteed that is a permutation of length .
A student is happy if his/her number is different from the number of his/her chair. In order to make all of his students happy, Dalton can repeatedly perform the following operation: choose two distinct students and swap their chairs. What is the minimum number of moves required to make all the students happy? One can show that, under the constraints of this problem, it is possible to make all the students happy with a finite number of moves.
A permutation of length 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).
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line contains a single integer () — the number of students.
The second line contains integers () — denotes the initial chair of student . It is guaranteed that is a permutation.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output the minimum number of moves required.
Note
In the first test case, both students are already happy, so Dalton can perform moves.
In the second test case, Dalton can swap the chairs of students and to get the array . Then he can swap chairs of students and to get the array . At this point all the students are happy, and he performed moves. It is impossible to perform the task with fewer moves.
In the third test case, by swapping the chairs of students and and then swapping the chairs of students and , Dalton gets the array in moves.
Samples
5
2
2 1
3
1 2 3
5
1 2 5 4 3
4
1 2 4 3
10
10 2 1 3 6 5 4 7 9 8
0
2
2
1
1
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |