Problems
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
i = 1,~ j = 3 \longrightarrow L = [1, 2, 4, 3] i = 2,~ j = 2 \longrightarrow L = [1, 2, 4, 3] i = 3,~ j = 4 \longrightarrow L = [1, 2, 3, 4]
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
Given the initial list, compute the cost of executing Reversort on it.
Input
The first line of the input gives the number of test cases,
Output
For each test case, output one line containing Case #, where
Example
3
4
4 2 1 3
2
1 2
7
7 6 5 4 3 2 1
Case #1: 6
Case #2: 1
Case #3: 12