#P13854. [CERC 2023] Jumbled Stacks
[CERC 2023] Jumbled Stacks
题目描述
We are given a set of cards, labelled from to , which are distributed into stacks . Each stack has a limited capacity: the -th stack, , can contain at most cards. The only way we can manipulate these cards is by taking the top card of a stack and moving it to the top of some other stack (as long as this wouldn’t exceed the capacity of the destination stack).
Using a sequence of such moves, we would like to rearrange the cards so that the first few stacks (0 or more) with the smallest indices are filled to capacity, the stack immediately after them is not filled to capacity (and may even be empty) and all stacks after that are completely empty. Moreover, if we stack together all the stacks from at the bottom to at the top, the cards should be ordered from smallest to largest, with at the bottom and at the top.
It is guaranteed that $n \leq \left( \sum_{i=1}^{k} C_i \right) - \max_{1 \leq i \leq k} C_i$.
Suppose we had cards on stacks, with capacities , , and with the following initial state: (from bottom to top; indicates an empty slot), , . Then the desired end state is , and .
输入格式
The first line contains two integers, (the number of cards) and (the number of stacks), separated by a space. The remaining lines describe the initial state of the stacks; the -th of these lines describes and contains integers, separated by spaces. The first of these integers is (the capacity of the stack ), the rest of them are the labels of the cards on , from bottom to top. If the stack contains fewer than cards (it could even be empty), the last few integers in the line will be .
输出格式
Print a sequence of moves that bring the stacks into the desired end state. For each move, output a line containing two integers, separated by a space: first the number of the stack from which the card is being moved and then the number of the stack to which it is being moved (the stacks are numbered from to ; the destination stack must not be the same as the source stack). The number of moves must not exceed . After the end of the sequence of moves, print a line containing “0 0” (without the quotation marks). If there are several possible solutions, you may output any of them.
6 3
4 2 3 0 0
3 4 1 6
3 5 0 0
2 3
2 3
1 2
1 2
3 1
2 1
2 1
3 2
3 1
2 3
1 3
2 1
3 2
3 2
0 0
提示
Comment
This is the example discussed earlier in the problem statement. The sample output shows a sequence of 14 moves which bring the stacks into the desired end state.