Note: The main parts of the statements of the problems "Reversort" and "Reversort Engineering" are identical, except for the last paragraph. The problems can otherwise be solved independently.
Reversort is an algorithm to sort a list of distinct integers in increasing order. The algorithm is based on the "Reverse" operation. Each application of this operation reverses the order of some contiguous part of the list.
The pseudocode of the algorithm is the following:
Reversort(L):
for i := 1 to length(L) - 1
j := position with the minimum value in L between i and length(L), inclusive
Reverse(L[i..j])
After
For example, for a list with 4 elements, the algorithm would perform 3 iterations.
Here is how it would process
, , ,
The most expensive part of executing the algorithm on our architecture is the Reverse operation.
Therefore, our measure for the cost of each iteration is simply the length of the sublist passed
to Reverse, that is, the value
In the example above, the iterations cost 3, 1, and 2, in that order, for a total of 6.
You are given a size
Input Specification
The first line of the input gives the number of test cases,
Output Specification
For each test case, if there is no list of size Case #x: IMPOSSIBLE
, where Case #x: y1 y2 ... yN
, where
If there are multiple solutions, you may output any one of them.
Limits
Time limit: 10 seconds.
Memory limit: 1 GB.
Test Set 1
Test Set 2
Sample Input
5
4 6
2 1
7 12
7 2
2 1000
Sample Output
Case #1: 4 2 1 3
Case #2: 1 2
Case #3: 7 6 5 4 3 2 1
Case #4: IMPOSSIBLE
Case #5: IMPOSSIBLE
Sample Case #1 is described in the statement above.
In Sample Case #2, the algorithm runs for only one iteration on the proposed output. In that iteration, reverse is applied to a sublist of size 1, therefore, its cost is 1.
In Sample Case #3, the first iteration reverses the full list, for a cost of 7. After that, the list is already sorted, but there are 5 more iterations, each of which contributes a cost of 1. Another valid output would be 7 5 4 3 2 1 6
. For that output, the first iteration has a cost of 6, the last one has a cost of 2, and all others have a cost of 1.
In Sample Case #4, Reversort will necessarily perform 6 iterations, each of which will have a cost of at least 1, so there is no way the total cost can be as low as required.
Comments