#P10488. [BAPC 2006 资格赛] Booksort

[BAPC 2006 资格赛] Booksort

Problem Description

Given nn books numbered from 11 to nn.

In the initial state, the books are in an arbitrary permutation.

In each operation, you may take a consecutive segment of books and insert this segment into some other position.

The target state is to arrange the books in order from 11 to nn.

Find the minimum number of operations needed.

Input Format

The first line contains an integer TT, meaning there are TT test cases.

Each test case contains two lines. The first line is an integer nn, meaning the number of books.

The second line contains nn integers, representing an arbitrary permutation of 1n1 \sim n.

Numbers on the same line are separated by spaces.

Output Format

For each test case, output the minimum number of operations.

If the minimum number of operations is greater than or equal to 55, output 5 or more.

Each result occupies one line.

3
6
1 3 4 6 2 5
5
5 4 3 2 1
10
6 8 5 3 4 7 2 9 1 10
2
3
5 or more

Hint

1T31 \le T \le 3, 1n151 \le n \le 15

Translated by ChatGPT 5