#P1372C. Omkar and Baseball
Omkar and Baseball
No submission language available for this problem.
Description
Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across sessions follow the identity permutation (ie. in the first game he scores point, in the second game he scores points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up!
Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on can yield but it cannot yield since the is in the same position.
Given a permutation of integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed .
An array is a subarray of an array if can be obtained from by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The first line of each test case contains integer () — the length of the given permutation.
The second line of each test case contains integers () — the initial permutation.
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The first line of each test case contains integer () — the length of the given permutation.
The second line of each test case contains integers () — the initial permutation.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.
Samples
Note
In the first permutation, it is already sorted so no exchanges are needed.
It can be shown that you need at least exchanges to sort the second permutation.
Perform special exchange on range ()
Perform special exchange on range ()