Bottom-Tier Reversals solution codeforces
You have a permutation: an arrayof distinct integers from to . The length of the permutation 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, you can choose any odd integer between and , inclusive, and set to .
Find a way to sortusing no more than reversals of the above kind, or determine that such a way doesn’t exist. The number of reversals doesn’t have to be minimized.
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 a single integer( ; is odd) — the length of the permutation.
The second line containsdistinct integers ( ) — the permutation itself.
It is guaranteed that the sum ofover all test cases does not exceed .
For each test case, if it’s impossible to sort the given permutation in at mostreversals, print a single integer .
Otherwise, print an integer( ), denoting the number of reversals in your sequence of steps, followed by integers ( ; is odd), denoting the lengths of the prefixes of to be reversed, in chronological order.
Note thatdoesn’t have to be minimized. If there are multiple answers, print any.
3 3 1 2 3 5 3 4 5 2 1 3 2 1 3
4 3 3 3 3 2 3 5 -1
In the first test case, the permutation is already sorted. Any even number of reversals of the lengthprefix doesn’t change that fact.
In the second test case, after reversing the prefix of lengththe permutation will change to , and then after reversing the prefix of length the permutation will change to .
In the third test case, it’s impossible to sort the permutation.
Solution: Click here