#P10327. [UESTCPC 2024] 汉诺塔排序问题
[UESTCPC 2024] 汉诺塔排序问题
Problem Description
Natsuzora is playing a Tower of Hanoi with special rules.
There are three pegs, , , and . Disks can be stacked on each peg. Disks may be moved between pegs, but the following rules must be followed:
- Only one disk can be moved at a time.
- Each move can only take the top disk of one peg and place it onto the top of another peg.
- On pegs and , it is not allowed to place any disk on top of another disk that is smaller than it.
- On peg , it is allowed to place a larger disk on top of a smaller disk.
At the beginning, Natsuzora stacks disks of distinct sizes in a random order on peg , leaving pegs and empty. Under the rules above, Natsuzora wants to know the minimum number of moves needed to move all disks from peg to peg .
Input Format
The first line contains an integer , representing the number of test cases.
For each test case, the first line contains an integer , representing the number of disks on peg in the initial state.
The second line of each test case contains integers , where is the diameter of the -th disk on peg from bottom to top. The testdata guarantees that is a permutation of length , meaning each integer from to appears exactly once in the sequence.
For all test cases, it is guaranteed that .
Output Format
For each test case, output one integer per line, representing the minimum number of moves required.
3
5
1 5 3 2 4
5
1 2 3 4 5
6
5 3 6 1 4 2
11
5
19
Hint
In the first sample, one feasible move sequence with the minimum number of moves is as follows ( means moving the top disk of peg to the top of peg ):
Translated by ChatGPT 5