Under construction

Still being built. Things may change

Pancake sorting

Interview

Time limit2sMemory limit512 MB

Summary
Sort a stack of up to 8 pancakes into descending order with the fewest suffix flips.
Level

Medium4 of 10

Topics
BFS, Brute force
Solved
No attempts yet

Problem

You are given a stack of NN pancakes, where 1≤N≤81 \le N \le 8. The pancake at the bottom has index 00 and the pancake at the top has index N−1N-1. The size of a pancake is its diameter, a positive integer. All pancakes in the stack have different sizes.

A stack AA of N=5N = 5 pancakes with sizes 3, 8, 7, 6 and 10 looks like this.

4 (top)        10
3               6
2               7
1               8
0 (bottom)      3
-----------------------
index i      A[i]

You want to sort the stack in descending order, so that the largest pancake sits at the bottom and the smallest sits at the top. Sorting can only be done by a sequence of pancake flips, written flip(i). The flip(i) operation inserts a spatula into the stack, lifts up the pancakes from index ii to index N−1N-1 and turns them over, so the order of the pancakes from index ii to index N−1N-1 is reversed.

For example, stack AA becomes stack BB via flip(0), stack BB becomes stack CC via flip(3), and stack CC becomes stack DD via flip(1). The goal is to reach a stack sorted in descending order, like stack EE, with as few flips as possible.

4 (top)      10  <--   3  <--   8  <--   6                 3
3             6        8  <--   3        7     ...         6
2             7        7        7        3                 7
1             8        6        6  <--   8                 8
0 (bottom)    3  <--  10       10       10                10
--------------------------------------------------------------
index i    A[i]     B[i]     C[i]     D[i]     ...      E[i]

Bill Gates, who founded Microsoft, has published exactly one research paper, and its subject is this pancake sorting problem.

Gates, W. and Papadimitriou, C. Bounds for Sorting by Prefix Reversal. Discrete Mathematics, 27, 47-57, 1979.

Given the starting configuration of NN pancakes, compute the minimum number of flips needed to sort them.

Input

The first line contains an integer TT, the number of test cases. Each of the next TT lines holds one test case. A test case starts with the number of pancakes NN, followed by NN integers giving the size of each pancake. The bottom pancake, the one with index 00, comes first in the sequence.

Output

For each test case, print on one line the minimum number of flips needed to sort that stack. The output therefore has TT lines with one integer per line.

Examples4

  1. Example 1

    Input
    2
    4 4 3 2 1
    8 8 7 6 5 4 1 2 3
    
    Expected output
    0
    1
    
  2. Example 2

    Input
    3
    4 4 3 2 1
    8 8 7 6 5 4 1 2 3
    5 5 1 2 4 3
    
    Expected output
    0
    1
    2
    
  3. Example 3

    Input
    2
    5 555555 111111 222222 444444 333333
    8 1000000 999999 999998 999997 999996 999995 999994 999993
    
    Expected output
    2
    0
    
  4. Example 4

    Input
    3
    5 555555 111111 222222 444444 333333
    8 1000000 999999 999998 999997 999996 999995 999994 999993
    5 3 8 7 6 10
    
    Expected output
    2
    0
    4