Strange Sort
Contest: Codeforces Round 740 (Div. 1, based on VK Cup 2021 - Final (Engine))
Difficulty rating: 3300
Tags: data structures, sortings

You have a permutation: an array $$$a = [a_1, a_2, \ldots, a_n]$$$ of distinct integers from $$$1$$$ to $$$n$$$. The length of the permutation $$$n$$$ is odd.

Consider the following algorithm of sorting the permutation in increasing order.

A helper procedure of the algorithm, $$$f(i)$$$, takes a single argument $$$i$$$ ($$$1 \le i \le n-1$$$) and does the following. If $$$a_i > a_{i+1}$$$, the values of $$$a_i$$$ and $$$a_{i+1}$$$ are exchanged. Otherwise, the permutation doesn't change.

The algorithm consists of iterations, numbered with consecutive integers starting with $$$1$$$. On the $$$i$$$-th iteration, the algorithm does the following:

- if $$$i$$$ is odd, call $$$f(1), f(3), \ldots, f(n - 2)$$$;
- if $$$i$$$ is even, call $$$f(2), f(4), \ldots, f(n - 1)$$$.

It can be proven that after a finite number of iterations the permutation will be sorted in increasing order.

After how many iterations will this happen for the first time?

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5 - 1$$$; $$$n$$$ is odd) — the length of the permutation.

The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — the permutation itself.

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

Output format:
For each test case print the number of iterations after which the permutation will become sorted in increasing order for the first time.

If the given permutation is already sorted, print $$$0$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first test case, the permutation will be changing as follows:

- after the $$$1$$$-st iteration: $$$[2, 3, 1]$$$;
- after the $$$2$$$-nd iteration: $$$[2, 1, 3]$$$;
- after the $$$3$$$-rd iteration: $$$[1, 2, 3]$$$.

In the second test case, the permutation will be changing as follows:

- after the $$$1$$$-st iteration: $$$[4, 5, 1, 7, 2, 3, 6]$$$;
- after the $$$2$$$-nd iteration: $$$[4, 1, 5, 2, 7, 3, 6]$$$;
- after the $$$3$$$-rd iteration: $$$[1, 4, 2, 5, 3, 7, 6]$$$;
- after the $$$4$$$-th iteration: $$$[1, 2, 4, 3, 5, 6, 7]$$$;
- after the $$$5$$$-th iteration: $$$[1, 2, 3, 4, 5, 6, 7]$$$.

In the third test case, the permutation is already sorted and the answer is $$$0$$$.

Example input:
3
3
3 2 1
7
4 5 7 1 3 2 6
5
1 2 3 4 5

Example output:
3
5
0

Editorial:
Let's draw a wall of $$$n$$$ towers of cubes, with the $$$i$$$-th tower having height $$$a_i$$$. For example, for $$$a = [4, 5, 7, 1, 3, 2, 6]$$$ the picture will look as follows ($$$1$$$ stands for a cube):
Note that applying $$$f(i)$$$ to the permutation (swapping $$$a_i$$$ and $$$a_{i+1}$$$ if $$$a_i > a_{i+1}$$$) is equivalent to applying $$$f(i)$$$ to each row of the above matrix independently (swapping cells $$$i$$$ and $$$i+1$$$ if the $$$i$$$-th cell is $$$1$$$ and the $$$i+1$$$-th cell is $$$0$$$).
Also note that in the final state, when the permutation is $$$[1, 2, \ldots, n]$$$, each row of the matrix is sorted in non-descending order too ($$$0$$$'s go before $$$1$$$'s), and vice versa — if each row is sorted, the permutation is sorted as well.
Thus, it's enough to find the number of iterations required to sort each row of the matrix, and the maximum of these numbers is the answer for the given permutation.
The rows of the matrix are $$$b_i = [a_i \ge x]$$$ for $$$x = 1, 2, \ldots, n$$$.
How to solve the problem for a sequence of $$$0$$$'s and $$$1$$$'s?
We can assume that the instances of $$$0$$$'s don't change their relative order, and the same for $$$1$$$'s. Let the positions of zeros in the initial sequence be $$$1 \le p_1 < p_2 < \ldots < p_m \le n$$$. The $$$i$$$-th zero from the left is moving towards position $$$i$$$ in the sorted sequence.
Let $$$s(i)$$$ be the number of steps it takes the $$$i$$$-th zero from the left to get to its final position $$$i$$$.
If the $$$i$$$-th zero is already in position $$$i$$$, then $$$s(i) = 0$$$.
Otherwise, if $$$i > 1$$$, note that $$$s(i) \ge s(i - 1) + 1$$$, because the $$$i$$$-th zero can only get to position $$$i$$$ after the $$$(i-1)$$$-th zero gets to position $$$i-1$$$.
Moreover, let there be $$$k_i$$$ ones to the left of the $$$i$$$-th zero in the initial sequence. Then $$$s(i) \ge k_i + (p_i \bmod 2)$$$, because the $$$0$$$ has to swap with every $$$1$$$ to the left of it, and also the first iteration is useless if $$$p_i$$$ is odd.
It turns out that $$$s(i) = \max(s(i - 1) + 1, k_i + (p_i \bmod 2))$$$ — the $$$i$$$-th zero either gets stuck into the $$$(i-1)$$$-th zero (and then $$$s_i = s(i - 1) + 1$$$), or consistently swaps with $$$1$$$'s on each iteration except for maybe the first (in which case $$$s_i = k_i + (p_i \bmod 2)$$$).
We are interested in $$$s(m)$$$. Let the number of $$$0$$$'s at the start of the initial sequence be $$$t$$$. It can be seen that $$$s(m) = \max \limits_{i=t+1}^m (k_i + (p_i \bmod 2) + (m - i))$$$, and this is exactly the number of iterations required.
Recall that we need to find the number of iterations for $$$n$$$ different binary sequences. However, these binary sequences are very similar to each other.
Let's maintain the values of $$$(k_i + (p_i \bmod 2) + (m - i))$$$ for all zeros in a segment tree (and, say, $$$-\infty$$$ for positions containing ones). Start with the sequence $$$b_i = [a_i \ge x]$$$ for $$$x = 1$$$ — that is, a sequence of all ones. As we increase $$$x$$$ by one, a single $$$1$$$ in $$$b$$$ gets replaced with $$$0$$$. We can handle these changes using a segment tree with "range add" and "range max". The time complexity of the solution is $$$O(n \log n)$$$.
(Another equivalent formula is: $$$s(m) = \max \limits_{i=l}^r c_1([b_1, b_2, \ldots, b_i]) + c_0([b_{i+1}, \ldots, b_n]) + (i \bmod 2) - 1$$$. Here $$$c_x(seq)$$$ is the number of $$$x$$$'s in $$$seq$$$; $$$l$$$ is the smallest position such that $$$c_1([b_1, b_2, \ldots, b_l]) > 0$$$; and $$$r$$$ is the largest position such that $$$c_0([b_{r+1}, \ldots, b_n]) > 0$$$. This formula can also be proven using induction. In short, consider this formula applied to the number of remaining iterations — the difference is that instead of $$$(i \bmod 2)$$$, we sometimes have $$$1 - (i \bmod 2)$$$, depending on the parity of the current iteration number. Consider all positions $$$i$$$ where the value is maximized. Then it can be shown that after one iteration the value for all such positions decreases by $$$1$$$: if the parity is incorrect, after one iteration the parity becomes correct; otherwise, observe that $$$b_i = 1$$$ and $$$b_{i+1} = 0$$$, which get swapped in the next iteration. Moreover, the value for all other positions has the same parity, increases by at most $$$1$$$, and thus doesn't become too big either.)