Under construction

Still being built. Things may change

Up and Down (Large)

Time limit5sMemory limit512 MB

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

Medium5 of 10

Topics
Greedy, Prefix sum
Solved
No attempts yet

Problem

You are given a sequence of distinct integers A=[A1,A2,…,AN]A = [A_1, A_2, \dots, A_N], and you want to rearrange it into an up and down sequence. A sequence is up and down when there is an index mm (1≤m≤N1 \le m \le N) with

A1<A2<⋯<Am>Am+1>⋯>AN.A_1 < A_2 < \dots < A_m > A_{m+1} > \dots > A_N.

If m=1m = 1 the whole sequence decreases, and if m=Nm = N the whole sequence increases.

You may swap two adjacent elements of the sequence, one swap at a time. 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. Each test case begins with a line containing one integer NN. The next line contains NN distinct integers A1,…,ANA_1, \dots, A_N separated by spaces.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤10001 \le N \le 1000
  • 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 swaps required to rearrange AA into an up and down sequence.

Hint

The sequence in the first example is already in the desired form (with m=N=3m = N = 3), so no swap is needed.

In the second example, swapping 3 and 7 produces an up and down sequence (with m=3m = 3).

Examples1

  1. Example 1

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