#P7115. [NOIP2020] 移球游戏

[NOIP2020] 移球游戏

Description

Xiao C is playing a ball moving game. There are n+1n + 1 poles in front of him, numbered from 11 to n+1n + 1. Poles 11, 22, \ldots, nn each initially have mm balls stacked from bottom to top, while pole n+1n + 1 initially has no balls. There are n×mn \times m balls in total, with nn colors, and each color has exactly mm balls.

Initially, the balls on a single pole may have many different colors. Xiao C’s task is to move all balls of the same color onto the same pole. This is the only goal, and there is no restriction on which pole each color finally ends up on.

Xiao C can achieve this goal using a sequence of operations. In one operation, he moves one ball from one pole to another. More specifically, moving a ball from pole xx to pole yy must satisfy:

  1. Pole xx has at least one ball.
  2. Pole yy has at most m1m - 1 balls.
  3. Only the top ball of pole xx can be moved to the top of pole yy.

Since the goal is not hard to achieve, Xiao C decided to make it more challenging: while achieving the goal, the total number of operations must not exceed 820000820000. In other words, Xiao C must complete the goal in at most 820000820000 operations.

Xiao C is stuck, but he believes you can solve it. Please output an operation plan to achieve Xiao C’s goal. There may be many valid plans; you only need to output any one of them. It is guaranteed that at least one valid plan exists.

Input Format

The first line contains two integers n,mn, m separated by spaces, representing the number of colors of balls and the number of balls of each color.
The next nn lines each contain mm integers separated by single spaces. For line ii, the integers give the colors of the balls on pole ii in order from bottom to top.

Output Format

This problem is judged with a special judge.
The first line of your output should contain only a single integer kk, indicating the number of operations in your plan. You must ensure that 0k8200000 \le k \le 820000.
The next kk lines should each contain two positive integers x,yx, y separated by a single space, meaning that this operation moves the top ball of pole xx to the top of pole yy. You must ensure that 1x,yn+11 \le x, y \le n + 1 and xyx \ne y.

2 3
1 1 2
2 1 2

6
1 3
2 3
2 3
3 1
3 2
3 2

见附件中的 ball/ball2.in
见附件中的 ball/ball2.ans
见附件中的 ball/ball3.in
见附件中的 ball/ball3.ans

Hint

[Sample #1 Explanation]

The contents in the poles are given as: for each pole, the colors of its balls are listed from bottom to top.

Operation Pole 11 Pole 22 Pole 33
Initial 1 1 21\ 1\ 2 2 1 22\ 1\ 2
1 31\ 3 1 11\ 1 22
2 32\ 3 2 12\ 1 2 22\ 2
22 2 2 12\ 2\ 1
3 13\ 1 1 1 11\ 1\ 1 2 22\ 2
3 23\ 2 2 22\ 2 22
2 2 22\ 2\ 2

[Constraints]

Test Point ID nn \le mm \le
121 \sim 2 22 2020
353 \sim 5 1010
686 \sim 8 5050 8585
9149 \sim 14 300300
152015 \sim 20 400400

For all test points, it is guaranteed that 2n502 \le n \le 50 and 2m4002 \le m \le 400.

[Checker]

To make it easier for contestants to test, we provide a checker.cpp file in the ball directory in the attachments. You can compile this program and use it to check your output file. However, note that it is not exactly the same as the checker used in the final judging. You also do not need to care about the exact details of its code.

The compile command is: g++ checker.cpp −o checker -std=c++11.

Usage of checker is: checker <inputfile> <outputfile>, where the parameters are the input file and your output file, respectively.

If the range of numbers in your output is invalid, the checker will print corresponding messages. If the range is correct but the plan is wrong, it will print brief error information:

  1. A x, meaning the operation is invalid at the xx-th operation.
  2. B x, meaning after all operations are executed, the balls on pole xx are invalid.

If your plan is correct, the checker will print OK.

Translated by ChatGPT 5