Bottom-Tier Reversals
Contest: Codeforces Round 740 (Div. 1, based on VK Cup 2021 - Final (Engine))
Difficulty rating: 2000
Tags: constructive algorithms, greedy

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.

You need to sort the permutation in increasing order.

In one step, you can choose any prefix of the permutation with an odd length and reverse it. Formally, if $$$a = [a_1, a_2, \ldots, a_n]$$$, you can choose any odd integer $$$p$$$ between $$$1$$$ and $$$n$$$, inclusive, and set $$$a$$$ to $$$[a_p, a_{p-1}, \ldots, a_1, a_{p+1}, a_{p+2}, \ldots, a_n]$$$.

Find a way to sort $$$a$$$ using no more than $$$\frac{5n}{2}$$$ reversals of the above kind, or determine that such a way doesn't exist. The number of reversals doesn't have to be minimized.

Input format:
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 a single integer $$$n$$$ ($$$3 \le n \le 2021$$$; $$$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 $$$2021$$$.

Output format:
For each test case, if it's impossible to sort the given permutation in at most $$$\frac{5n}{2}$$$ reversals, print a single integer $$$-1$$$.

Otherwise, print an integer $$$m$$$ ($$$0 \le m \le \frac{5n}{2}$$$), denoting the number of reversals in your sequence of steps, followed by $$$m$$$ integers $$$p_i$$$ ($$$1 \le p_i \le n$$$; $$$p_i$$$ is odd), denoting the lengths of the prefixes of $$$a$$$ to be reversed, in chronological order.

Note that $$$m$$$ doesn't have to be minimized. If there are multiple answers, print any.

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

Note:
In the first test case, the permutation is already sorted. Any even number of reversals of the length $$$3$$$ prefix doesn't change that fact.

In the second test case, after reversing the prefix of length $$$3$$$ the permutation will change to $$$[5, 4, 3, 2, 1]$$$, and then after reversing the prefix of length $$$5$$$ the permutation will change to $$$[1, 2, 3, 4, 5]$$$.

In the third test case, it's impossible to sort the permutation.

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

Example output:
4
3 3 3 3
2
3 5
-1

Editorial:
First of all, consider what happens when we reverse a prefix of odd length $$$p$$$. Elements $$$a_{p+1}$$$ to $$$a_n$$$ don't move at all, and for each $$$i$$$ from $$$1$$$ to $$$p$$$, $$$a_i$$$ moves to $$$a_{p-i+1}$$$. Note that $$$i$$$ and $$$p-i+1$$$ have the same parity: therefore, no element can ever change the parity of its position. In the final sorted permutation, we need to have $$$a_i = i$$$ for all $$$i$$$: that is, the parity of each element's position must match the parity of its value.
This leads to the following necessary condition: for each $$$i \in [1, n]$$$, $$$a_i \bmod 2 = i \bmod 2$$$. If for any $$$i$$$ this doesn't hold, the permutation can not be sorted.
It turns out this condition is also sufficient.
Let's devise a procedure to sort a permutation of odd length $$$n$$$. If $$$a_n = n$$$ and $$$a_{n - 1} = n - 1$$$, we don't have to touch $$$a_{n-1}$$$ and $$$a_n$$$ ever again, and we can proceed to sorting a permutation of length $$$n-2$$$. Can we actually move $$$n$$$ and $$$n-1$$$ to their final positions with a simple sequence of steps?
Indeed we can. Here is one way to do this in exactly $$$5$$$ steps:
• Let $$$a_x = n$$$ (note that $$$x$$$ is odd). Reverse a prefix of length $$$x$$$ to move $$$n$$$ to position $$$1$$$.
• Let $$$a_y = n-1$$$ (note that $$$y$$$ is even). Reverse a prefix of length $$$y-1$$$ to move $$$n$$$ to position $$$y-1$$$.
• Reverse a prefix of length $$$y+1$$$ to move $$$n-1$$$ to position $$$2$$$ and $$$n$$$ to position $$$3$$$.
• Reverse a prefix of length $$$3$$$ to move $$$n$$$ to position $$$1$$$ ($$$n-1$$$ stays at position $$$2$$$).
• Reverse a prefix of length $$$n$$$ to move $$$n$$$ to position $$$n$$$ and $$$n-1$$$ to position $$$n-1$$$, as desired.
We can use this procedure $$$\frac{n-1}{2}$$$ times to first move $$$n$$$ and $$$n-1$$$ to their final positions, then $$$n-2$$$ and $$$n-3$$$, and so on. This solution requires exactly $$$\frac{5(n-1)}{2}$$$ steps.