Codeforces Round 655 (Div. 2)
C
Omkar and Baseball
Codeforces
1500
原题
← BD →
翻译中…

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!

翻译中…

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.

翻译中…

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}$.

翻译中…

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.

翻译中…

Input

翻译中…

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.

翻译中…

The first line of each test case contains integer $n$ ($1 \leq n \leq 2 \cdot 10^5$)  — the length of the given permutation.

翻译中…

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.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.

翻译中…

Note

翻译中…

In the first permutation, it is already sorted so no exchanges are needed.

翻译中…

It can be shown that you need at least $2$ exchanges to sort the second permutation.

翻译中…

$[3, 2, 4, 5, 1, 6, 7]$

翻译中…

Perform special exchange on range ($1, 5$)

翻译中…

$[4, 1, 2, 3, 5, 6, 7]$

翻译中…

Perform special exchange on range ($1, 4$)

翻译中…

$[1, 2, 3, 4, 5, 6, 7]$

翻译中…

样例

Input #1
2
5
1 2 3 4 5
7
3 2 4 5 1 6 7
Output #1
0
2
InputCopy OutputCopy
题目信息
题号
1372C
时间限制
2 s
内存限制
256 MB
标签
构造
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。