1372C.md (2412B)
1 Source: https://codeforces.com/problemset/problem/1372/C 2 3 C. Omkar and Baseball 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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 $n$ sessions follow the identity permutation (ie. in the first game he scores $1$ point, in the second game he scores $2$ points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! 22 23 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 $[1,2,3]$ can yield $[3,1,2]$ but it cannot yield $[3,2,1]$ since the $2$ is in the same position. 24 25 Given a permutation of $n$ 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 $10^{18}$. 26 27 An array $a$ is a subarray of an array $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. 28 29 Input 30 31 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. 32 33 The first line of each test case contains integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the given permutation. 34 35 The second line of each test case contains $n$ integers $a_{1},a_{2},...,a_{n}$ ($1 \leq a_{i} \leq n$) — the initial permutation. 36 37 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 38 39 Output 40 41 For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. 42 43 Example 44 45 Input 46 47 ``` 48 2 49 50 5 51 52 1 2 3 4 5 53 54 7 55 56 3 2 4 5 1 6 7 57 ``` 58 59 Output 60 61 ``` 62 0 63 2 64 ``` 65 66 Note 67 68 In the first permutation, it is already sorted so no exchanges are needed. 69 70 It can be shown that you need at least $2$ exchanges to sort the second permutation. 71 72 $[3, 2, 4, 5, 1, 6, 7]$ 73 74 Perform special exchange on range ($1, 5$) 75 76 $[4, 1, 2, 3, 5, 6, 7]$ 77 78 Perform special exchange on range ($1, 4$) 79 80 $[1, 2, 3, 4, 5, 6, 7]$