#P15346. [TOIP 2025] 煎餅攤
[TOIP 2025] 煎餅攤
Problem Description
Amai set up a stall at a holiday market to sell pancakes.
Because everyone eats different amounts, he decided to sell different pancake sizes. From smallest to largest, they are labeled from to , and he planned to stack pancakes of the same size into one pile for easy shopping.
Each pile can contain at most pancakes, otherwise it will collapse because it is too tall.
Every time Amai finishes cooking a pancake, he puts it on the stall counter. However, because he was too rushed, pancakes of different sizes were stacked together in the same pile.
When he noticed, there were already piles on the counter, each pile had exactly pancakes, and each size also had exactly pancakes.
Amai wants to use a pancake spatula to move these pancakes and restore the arrangement he originally planned.
He can only adjust the pancake positions using the following actions:
- Insert the spatula into some pile, between two pancakes.
- Move all pancakes above the spatula to the top of another pile (the order of pancakes above the spatula remains unchanged).
Because the counter space is limited, besides the existing piles, the remaining space can only hold one more pile.
Also, during the process, no pile may ever exceed pancakes, otherwise that pile will collapse.
Please help Amai rearrange the pancakes so that pancakes of the same size are in the same pile, and smaller pancakes are placed more to the left.
For example, if , , the initial piles are shown in the figure below (the far right is the remaining empty space on the counter):
:::align{center}
:::
One possible sequence of moves is as follows. Insert the spatula into the leftmost pile between the bottom two pancakes, and move them to the remaining empty space on the right:
:::align{center}
:::
Insert the spatula into the middle pile between the bottom two pancakes, and move them to the leftmost pile:
:::align{center}
:::
Move the top pancake of the leftmost pile to the middle pile:
:::align{center}
:::
Then move the two pancakes in the rightmost pile to pile and pile respectively, and Amai’s desired arrangement is completed:
:::align{center}
:::
Please write a program to help Amai move pancakes of various sizes to the desired positions.
Since there can be many valid ways to move them, you may output any feasible sequence of moves, but the number of moves must be within .
Input Format
$$\begin{aligned} &m \; n \\ &a_{1,1} \; a_{1,2} \; \cdots \; a_{1,m} \\ &a_{2,1} \; a_{2,2} \; \cdots \; a_{2,m} \\ &\vdots \\ &a_{n,1} \; a_{n,2} \; \cdots \; a_{n,m} \end{aligned}$$- means there are currently piles of pancakes.
- means each pile currently has exactly pancakes, and each pancake size also has exactly pancakes.
- means the size of the -th pancake (from bottom to top) in the -th pile (from left to right).
Output Format
$$\begin{aligned} &c \\ &s_1 \; k_1 \; t_1 \\ &s_2 \; k_2 \; t_2 \\ &\vdots \\ &s_c \; k_c \; t_c \end{aligned}$$- is the total number of moves.
- , , mean that in the -th move, the top pancakes are moved from the -th pile (from left to right) to the -th pile (from left to right). (Pile is the empty space at the beginning.)
- .
- , and .
- and must not exceed the current number of pancakes in pile .
3 2
1 2 1
2 1 2
5
1 2 3
2 2 1
1 1 2
3 1 1
3 1 2
Hint
Constraints
- .
- .
- .
- All input numbers are integers.
- It is guaranteed that each number from to appears exactly times in .
Scoring
This problem has three subtasks, with restrictions as follows. Each subtask may contain one or more testdata, and the score of the subtask is the minimum score among all its testdata.
| Subtask | Score | Additional Input Constraints |
|---|---|---|
| 1 | 8 | 。 |
| 2 | 37 | 。 |
| 3 | 55 | No additional constraints. |
For this problem, if you output any valid solution for a testdata, you will receive full score for that testdata. However, if your output has any of the following invalid cases, that testdata will receive points:
- The number of moves is too large.
- Numbers are out of range.
- The number of moved pancakes is greater than the number of pancakes in the source pile.
- After all moves, pancakes of size are not all in pile .
In addition, if during the moving process any pile ever exceeds pancakes, then that testdata will be scored as of the original score for the testdata group.
Translated by ChatGPT 5