#P17825. [COCI 2026/2027 #1] 三堆箱子 / Tri
[COCI 2026/2027 #1] 三堆箱子 / Tri
Problem Description
Ana works in a warehouse that has three piles of boxes, labelled , and . Each box has a number from to written on it, and every number appears on exactly one box.
At the beginning, all the boxes are stacked on top of each other on pile , in some order, while piles and are empty.
The boxes are heavy, so Ana moves them one at a time. In one move, she may take the box from the top of one pile and put it on top of a different pile. She must not touch a box that is not on top of its pile, or the pile would fall over.
Ana wants all the boxes to end up on pile , stacked in increasing order from bottom to top: the box with number at the bottom, the box with number on top of it, and so on, up to the box with number at the top. Piles and must be empty at the end.
Ana is short on time, so she may make at most moves. Help her find a sequence of moves that achieves this arrangement. The number of moves does not have to be the smallest possible, but it must not be greater than .
Input Format
The first line contains positive integers and (, ), the number of boxes and the maximum allowed number of moves.
The second line contains distinct positive integers (), the numbers written on the boxes on pile , listed from bottom to top. In other words, is the number on the lowest box, and is the number on the box at the top of the pile.
Output Format
In the first line, output an integer (), the number of moves Ana should make.
In the -th of the following lines, output two different numbers and () describing the -th move: Ana takes the box from the top of pile and puts it on top of pile . Pile must not be empty when the move is made.
After all moves, the boxes on pile must be in the order from bottom to top, and piles and must be empty. If the boxes are already stacked this way at the beginning, you may output .
If there are multiple solutions, output any of them.
2 1000
2 1
4
1 2
1 3
2 1
3 1
3 1000
3 1 2
7
1 2
1 3
1 2
3 1
2 3
2 1
3 1
4 1000
1 2 3 4
0
Hint
Clarification of the first example
The box with number is on top, above the box with number . Ana first moves box to pile , and then box to pile . After that, she puts box back on pile , and then box on top of it, so that box is at the bottom and box is at the top.
Clarification of the second example
We write the contents of each pile as a sequence of numbers from bottom to top. At the beginning, pile contains , so the box with number is on top, and piles and are empty. The table shows the state of all piles after each move:
| Move | Pile 1 | Pile 2 | Pile 3 |
|---|---|---|---|
After the seventh move, pile contains boxes from bottom to top, and piles and are empty, so the arrangement is correct. This is not the only correct sequence of moves, nor is it the shortest one.
Clarification of the third example
The boxes on pile are already in the order from bottom to top, and piles and are empty. This is exactly the arrangement Ana wants, so she does not need to make any moves, and is a correct answer.
Scoring
| Subtask | Points | Constraints |
|---|---|---|
| 1 | 18 | , |
| 2 | 31 | |
| 3 | 54 | |
| 4 | 7 |