#P15346. [TOIP 2025] 煎餅攤

    ID: 17412 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>2025Special Judge台湾

[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 nn different pancake sizes. From smallest to largest, they are labeled from 11 to nn, and he planned to stack pancakes of the same size into one pile for easy shopping.

Each pile can contain at most mm 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 nn piles on the counter, each pile had exactly mm pancakes, and each size also had exactly mm 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 nn piles, the remaining space can only hold one more pile.
Also, during the process, no pile may ever exceed mm 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 n=2n=2, m=3m=3, the initial 22 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 11 and pile 22 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 9nm9nm.

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}$$
  • nn means there are currently nn piles of pancakes.
  • mm means each pile currently has exactly mm pancakes, and each pancake size also has exactly mm pancakes.
  • ai,ja_{i,j} means the size of the jj-th pancake (from bottom to top) in the ii-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}$$
  • cc is the total number of moves.
  • sis_i, kik_i, tit_i mean that in the ii-th move, the top kik_i pancakes are moved from the sis_i-th pile (from left to right) to the tit_i-th pile (from left to right). (Pile n+1n+1 is the empty space at the beginning.)
  • 0≤c≤9nm0 \le c \le 9nm.
  • 1≤si,ti≤n+11 \le s_i, t_i \le n+1, and si≠tis_i \neq t_i.
  • ki≥1k_i \ge 1 and must not exceed the current number of pancakes in pile sis_i.
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

  • 1≤n≤501 \le n \le 50.
  • 1≤m≤501 \le m \le 50.
  • 1≤ai,j≤n1 \le a_{i,j} \le n.
  • All input numbers are integers.
  • It is guaranteed that each number from 11 to nn appears exactly mm times in aa.

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 m=1m = 1。
2 37 n=2n = 2。
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 00 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 ii are not all in pile ii.

In addition, if during the moving process any pile ever exceeds mm pancakes, then that testdata will be scored as 30%30\% of the original score for the testdata group.

Translated by ChatGPT 5