Under construction

Still being built. Things may change

Up and Down Sequence

Interview

Time limit5sMemory limit512 MB

Summary
Count the fewest adjacent swaps that turn distinct numbers into a sequence that rises to one peak and then falls.
Level

Medium5 of 10

Topics
Brute force, Sorting
Solved
No attempts yet

Problem

You are given a sequence A=[A1,A2,…,AN]A = [A_1, A_2, \dots, A_N] of NN distinct integers. Rearrange it into an up and down sequence, that is, a sequence satisfying A1<A2<⋯<Am>Am+1>⋯>ANA_1 < A_2 < \dots < A_m > A_{m+1} > \dots > A_N for some index mm with 1≤m≤N1 \le m \le N. The values increase up to one position and decrease after it.

Only one operation is allowed. You may swap two adjacent elements of the sequence. Find the minimum number of such swaps needed to reach an up and down sequence.

Input

The first line contains the number of test cases TT. TT test cases follow. The first line of each test case contains the integer NN, and the second line contains NN distinct integers A1,A2,…,ANA_1, A_2, \dots, A_N separated by spaces.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤101 \le N \le 10
  • 1≤Ai≤1091 \le A_i \le 10^9
  • The AiA_i are pairwise distinct.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the minimum number of adjacent swaps needed to rearrange AA into an up and down sequence.

Hint

In the first case of the sample, the sequence is already up and down (with m=N=3m = N = 3), so no swap is needed. In the second case, swapping 3 and 7 gives 1 8 10 7 3, which is up and down with m=3m = 3.

Examples3

  1. Example 1

    Input
    2
    3
    1 2 3
    5
    1 8 10 3 7
    
    Expected output
    Case #1: 0
    Case #2: 1
    
  2. Example 2

    Input
    4
    1
    1
    1
    1000000000
    2
    1000000000 1
    2
    1 1000000000
    
    Expected output
    Case #1: 0
    Case #2: 0
    Case #3: 0
    Case #4: 0
    
  3. Example 3

    Input
    4
    5
    5 4 1 3 2
    6
    3 1 4 1000000000 5 2
    7
    7 6 5 1 2 3 4
    8
    4 3 2 1 5 6 7 8
    
    Expected output
    Case #1: 2
    Case #2: 1
    Case #3: 6
    Case #4: 6