#P3207. [HNOI2010] 物品调度
[HNOI2010] 物品调度
Description
Finding a job is not easy. Lostmonkey finally managed to get an entry-level assembly line operator position at fsk. There are positions on the line, numbered from to . Initially, position is empty, and each other position holds a box numbered . Lostmonkey needs to rearrange these boxes according to the following rules.
The rules are determined by five numbers, , where is the final position of the empty slot.
First, generate a sequence with , and .
Next, starting from the first box, generate the final position for each box in order: , where are non-negative integers chosen by you so that the -th box does not share the same position as any previous box, and must not be .
If there are multiple sequences that satisfy the requirements, you must choose the one with the lexicographically smallest . If is the same, choose the lexicographically smallest . In this way, you obtain the final positions for all boxes. Each move allows you to move any box into the empty slot; after moving, the original position of that box becomes the empty slot.
Find the minimum number of moves required to move all boxes to their final positions.
Input Format
The first line contains an integer , the number of test cases.
Then follow lines. Each line contains six numbers, , as described above.
Output Format
For each test case, output one number on a single line, the minimum number of moves.
1
8 3 5 2 7 4
6
Hint
[Sample Explanation]
The final positions of boxes through are: .
Constraints
- For of the testdata, .
- For of the testdata, , , .
- All other numbers are positive integers not exceeding .
Additional Hint
The computation may exceed the integer range.
Translated by ChatGPT 5
京公网安备 11011102002149号