You are given a sequence of distinct integers A = [A_{1}, A_{2}, ..., A_{N}], and would like to rearrange it into an up and down sequence (one where A_{1} < A_{2} < ... < A_{m} > A_{m+1} > ... > A_{N} for some index m, with m between 1 and N inclusive).
The rearrangement is accomplished by swapping two adjacent elements of the sequence at a time. Predictably, you are particularly interested in the minimum number of such swaps needed to reach an up and down sequence.
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing a single integer: N. The next line contains N distinct integers: A_{1}, ..., A_{N}.
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the minimum number of swaps required to rearrange A into an up and down sequence.
1 ≤ T ≤ 100.
1 ≤ A_{i} ≤ 10^{9}
The A_{i} will be pairwise distinct.
1 ≤ N ≤ 10.
1 ≤ N ≤ 1000.
Input |
Output |
2 3 1 2 3 5 1 8 10 3 7 |
Case #1: 0 Case #2: 1 |
In the first case, the sequence is already in the desired form (with m=N=3) so no swaps are required.
In the second case, swapping 3 and 7 produces an up and down sequence (with m=3).
Points | Correct | Attempted |
---|---|---|
7pt | 1728 | 2380 |
11pt | 1351 | 1426 |