Manhattan Sort
Time limit1sMemory limit128 MB
Sort each sequence of distinct integers with swaps that cost the distance between positions and report the minimum total cost.
Problem
You are given a sequence of distinct integers. Sort it into ascending order at the lowest possible cost, using only one operation.
The Manhattan swap: it takes the elements and sitting at positions and , exchanges them, and costs .
For example, the sequence becomes sorted after a single Manhattan swap. Exchange the first element with the last one and pay , the distance between their positions.
Input
The first line contains an integer , the number of test cases. Each test case consists of two lines. The first line contains one integer (), the length of the sequence . The second line contains the elements of , separated by spaces. All elements are distinct and fit in a 32 bit signed integer.
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the minimum cost of sorting the sequence into ascending order using only Manhattan swaps.