A - Container Handling with Cranes 解説 /

実行時間制限: 3 sec / メモリ制限: 1024 MB

問題文

N\times N マスのコンテナターミナルがある。 一番左上のマスの座標を (0,0) とし、そこから下方向に i マス、右方向に j マス進んだ先のマスの座標を (i,j) とする。 0 から N^2-1 の番号が振られた N^2 個のコンテナが運び込まれてくるので、N 台のクレーンを操作することで、指定された順番で搬出してほしい。

マップの左端と右端には、それぞれ以下のような搬入口・搬出口が存在する。

  • 搬入口: 左端の各マスは搬入口であり、各搬入口からはコンテナがそれぞれ N 個ずつ順番に運び込まれてくる。事前にどの番号のコンテナが運び込まれて来るかが分かっており、(i,0) にある搬入口から j 番目に運び込まれるコンテナの番号は A_{i,j} である。
  • 搬出口: 右端の各マスは搬出口であり、各搬出口に置かれたコンテナは即座に外部へ運び出される。(i,N-1) にある搬出口からは、N\times i,N\times i+1,\cdots,N\times i+N-1 番のコンテナをこの順番で搬出したい。

搬入口、搬出口を含む各マスには高々 1 つのコンテナを配置することが出来る。 搬出口以外に置かれたコンテナは搬出されないため、順番を調整するための一時置き場として利用出来る。

クレーンは 1 台の大クレーンと、N-1 台の小クレーンの 2 種類が存在する。初期状態で大クレーンは (0,0) のマスに、小クレーンは (1,0), (2,0), \cdots, (N-1,0) の各マスに 1 台ずつ配置されている。

各クレーンはコンテナを掴む、離す、隣接マスに移動する、といった操作が可能である。 コンテナが置かれていないマスには常にクレーンを移動可能であり、コンテナを掴んでいない場合はコンテナが置かれているマスにも移動出来る。 一方で、コンテナを掴んでいる状態でコンテナが置かれているマスに移動出来るかどうかは、クレーンの種類によって異なる。

大クレーンはコンテナを高く持ち上げて運ぶため、コンテナを掴んでいる状態でも他のコンテナが置かれているマスに移動することが出来る。 小クレーンの場合はコンテナを持ち上げる高さが低いため、コンテナを掴んでいる状態では他のコンテナが置かれているマスに移動することが出来ない。

毎ターン、各クレーンごとに独立に以下の操作から 1 つを選んで実行することが出来る。

  • P: 現在位置に存在するコンテナを掴む。すでにコンテナを掴んでいる場合や、現在位置にコンテナが存在しない場合は WA となる。
  • Q: 掴んでいるコンテナを離し、現在位置に配置する。コンテナを掴んでいない場合や、現在位置に既にコンテナが存在する場合は WA となる。
  • U, D, L, R: 上下左右に隣接するマスへ移動する。小クレーンかつコンテナを掴んでいる状態の場合は、移動先のマスにコンテナが存在してはならない。N\times N マスの外へ移動するような操作を行うことは出来ない。
  • .: 何もせずにその場に留まる。
  • B: クレーンを爆破する。コンテナを掴んでいる状態では爆破することは出来ない。爆破したクレーンは盤面から排除され、以後 . 以外の操作をすることは出来ない。

禁止されている操作を行った場合、WA となる。

それぞれのクレーンはコンテナを掴んでいるかどうかに関わらず、重なったり、すれ違うことは出来ない。 すなわち、同じマスに複数のクレーンが存在してはならず、2 つのクレーンがお互いの位置を交換するように移動することは出来ない。

各クレーンの操作は同時に行われるため、マス p に存在するクレーンがマス q に移動するのと同じターンに、マス q に存在するクレーンをマス r (r\neq p) に移動させたり爆破したりするような操作が可能である。ただし、r=p の場合はすれ違う移動となるため不可能である。

各ターンは以下の 3 ステップからなる。

  1. 運び込まれてくるコンテナがまだ残っている各搬入口について、そのマスにコンテナ及びコンテナを掴んだ状態のクレーンが存在しない場合、次の順番のコンテナがそのマスに配置される。
  2. 各クレーンの操作を行う。
  3. 各搬出口について、そのマスにコンテナが配置されている場合、そのコンテナを搬出して盤面から取り除く。

操作は最大で 10000 ターン繰り返すことが出来る。 短いターン数で指定された順番に近い順にコンテナを搬出出来るような操作列を求めてほしい。

得点

以下のように M_0, M_1, M_2, M_3 の値を定める。

  • M_0: 出力した操作列のターン数。
  • M_1: 正しい搬出口から搬出したコンテナの転倒数の総和。すなわち、(i,N-1) の搬出口から搬出されたコンテナのうち、番号 bN\times i\leq b<N\times (i+1) を満たすようなものを、搬出した順に B_{i,0},\cdots,B_{i,n_i-1} としたとき、0\leq j < k < n_iB_{i,j}>B_{i,k} を満たすような組 (i,j,k) の総数が M_1 である。
  • M_2: 間違った搬出口から搬出したコンテナの総数。すなわち、各搬出口 (i,N-1) から搬出されたコンテナのうち、番号 bN\times i \leq b < N\times (i+1) を満たさないものの数を数え、その総和が M_2 である。
  • M_3: 搬出されなかったコンテナの総数。

このとき、以下の絶対スコアが得られる。 絶対スコアは小さければ小さいほど良い。

\[ M_0+10^2M_1+10^4M_2+10^6M_3 \]

各テストケース毎に、\mathrm{round}(10^9\times \frac{全参加者中の最小絶対スコア}{自身の絶対スコア})相対評価スコアが得られ、その和が提出の得点となる。

最終順位はコンテスト終了後に実施されるより多くの入力に対するシステムテストにおける得点で決定される。 暫定テスト、システムテストともに、一部のテストケースで不正な出力や制限時間超過をした場合、そのテストケースの相対評価スコアは0点となり、そのテストケースにおいては「全参加者中の最小絶対スコア」の計算から除外される。 システムテストは CE 以外の結 果を得た一番最後の提出に対してのみ行われるため、最終的に提出する解答を間違えないよう注意せよ。

テストケース数

  • 暫定テスト: 50個
  • システムテスト: 2000個、コンテスト終了後に seeds.txt (sha256=502228d18e6d0d00f92654ad85f13945fc1d2865f69df352aee061a53ebf3cfa) を公開

相対評価システムについて

暫定テスト、システムテストともに、CE 以外の結果を得た一番最後の提出のみが順位表に反映される。 相対評価スコアの計算に用いられる各テストケース毎の全参加者中の最小絶対スコアの算出においても、順位表に反映されている最終提出のみが用いられる。

順位表に表示されているスコアは相対評価スコアであり、新規提出があるたびに、相対評価スコアが再計算される。 一方、提出一覧から確認出来る各提出のスコアは各テストケース毎の絶対スコアをそのまま足し合わせたものであり、相対評価スコアは表示されない。 最新以外の提出の、現在の順位表における相対評価スコアを知るためには、再提出が必要である。 不正な出力や制限時間超過をした場合、提出一覧から確認出来るスコアは0となるが、順位表には正解したテストケースに対する相対スコアの和が表示されている。

実行時間について

実行時間には多少のブレが生じます。 また、システムテストでは同時に大量の実行を行うため、暫定テストに比べて数%程度実行時間が伸びる現象が確認されています。 そのため、実行時間制限ギリギリの提出がシステムテストでTLEとなる可能性がありま す。 プログラム内で時間を計測して処理を打ち切るか、実行時間に余裕を持たせるようお願いします。


入力

入力は以下の形式で標準入力から与えられる。

N
A_{0,0} \cdots A_{0,N-1}
\vdots
A_{N-1,0} \cdots A_{N-1,N-1}
  • 全てのテストケースで N = 5 で固定である。
  • A_{i,j}(i,0) の搬入口から j 番目に搬入されるコンテナの番号であり、0\leq A_{i,j}\leq N^2-1 を満たす整数値である。全ての A_{i,j} の値は異なる。

出力

以下の形式で標準出力に出力せよ。

S_0
\vdots
S_{N-1}

S_iPQUDLR.B のみからなる文字列であり、その t 文字目は初期位置 (i,0) のクレーンに対する t ターン目の操作を表す。 出力した各文字列の長さは 1 以上 10000 以下でなければならない。 各文字列の長さは異なってもよく、その場合は最も長い文字列に合わせて文字列の末尾に . が追加される。

例を見る

入力生成方法

搬入順 A_{i,j} は、0,1,\cdots,N^2-1 をランダムな順番にシャッフルし、N 個ずつに区切ることにより生成される。

ツール(入力ジェネレータ・ビジュアライザ)

コンテスト期間中に、ビジュアライズ結果の共有や、解法・考察に関する言及は禁止されています。ご注意下さい。


入力例 1

5
24 10 17 15 13
14 11 2 1 5
7 9 6 21 20
8 4 19 3 16
18 23 22 0 12

出力例 1

PRDDDDRRRQLLLUUPRRRUQ
B
PRQB
PRRRRUUQB
PRRRRQB

Problem Statement

There is a container terminal with an N \times N grid. Let (0,0) be the coordinates of the top-left square and (i,j) be the coordinates of the square located i squares down and j squares to the right from there. N^2 containers, numbered from 0 to N^2-1, will be brought into the terminal. Using N cranes, please manage to dispatch the containers in the specified order.

There are Receiving Gates and Dispatch Gates on the left and right edges of the map, respectively:

  • Receiving Gates: Each square on the left edge is a Receiving Gate, and from each Receiving Gate, N containers are brought in sequentially. It is known in advance which containers will be brought in. The j-th container brought in from the Receiving Gate at (i,0) has the number A_{i,j}.
  • Dispatch Gates: Each square on the right edge is a Dispatch Gate, and containers placed at each Dispatch Gate are immediately dispatched externally. From the Dispatch Gate at (i,N-1), containers numbered N \times i, N \times i+1, \ldots, N \times i+N-1 are to be dispatched in this order.

Each square, including the Receiving Gates and Dispatch Gates, can hold at most one container. Squares other than the Dispatch Gates can be used as temporary storage to adjust the order of dispatch since containers placed in these squares will not be dispatched.

There are two types of cranes: one large crane and N-1 small cranes. Initially, the large crane is placed in the square (0,0), and each small crane is placed in the squares (1,0), (2,0), \ldots, (N-1,0).

Each crane can perform actions such as picking up a container, releasing a container, and moving to an adjacent square. Cranes can always move to squares that do not have containers. If not carrying a container, cranes can also move to squares with containers. However, whether a crane carrying a container can move to a square with another container depends on the type of crane.

The large crane can lift containers high, so it can move to squares with other containers even while carrying a container. Small cranes, on the other hand, lift containers to a lower height and cannot move to squares with other containers while carrying a container.

Each turn, for each crane, you can independently perform one of the following actions:

  • P: Pick up the container at the current square. If the crane is already holding a container or there is no container at the current square, this results in WA.
  • Q: Release the container being held and place it at the current square. If the crane is not holding a container or there is already a container at the current square, this results in WA.
  • U, D, L, R: Move to the adjacent square in the up, down, left, or right direction. If a small crane is carrying a container, the destination square must not have a container. Cranes cannot move outside the N \times N grid.
  • .: Do nothing and stay in the current square.
  • B: Bomb the crane. If the crane is holding a container, it cannot bomb. The crane is removed from the grid and can no longer perform any actions except ..

If you perform a prohibited action, it results in WA.

Each crane cannot overlap or pass each other, regardless of whether they are holding containers. In other words, multiple cranes cannot occupy the same square, and two cranes cannot move in such a way that they swap their positions.

Since the actions of each crane are performed simultaneously, it is possible to move the crane from square p to square q in the same turn as moving the crane from square q to square r (r \neq p) or bombing the crane at square q. However, if r = p, it results in an invalid move because it would be a passing movement.

Each turn consists of the following three steps:

  1. For each Receiving Gate where containers are still to be brought in, if there is neither a container nor a crane holding a container at that square, the next container in sequence is placed in that square.
  2. Perform the actions of each crane.
  3. For each Dispatch Gate, if there is a container at that square, dispatch the container and remove it from the grid.

Operations can be repeated for up to 10000 turns. Your task is to find a sequence of operations that dispatches the containers in the specified order as closely as possible within a short number of turns.

Scoring

Define the values M_0, M_1, M_2, and M_3 as follows:

  • M_0: The number of turns in the output sequence of operations.
  • M_1: The total number of inversions for containers dispatched from the correct Dispatch Gate. Specifically, for the containers dispatched from the Dispatch Gate at (i, N-1), let B_{i,0}, \cdots, B_{i,n_i-1} be the sequence of container numbers b that satisfy N \times i \leq b < N \times (i+1), in the order they were dispatched. Then, M_1 is the total number of pairs (i, j, k) such that 0 \leq j < k < n_i and B_{i,j} > B_{i,k}.
  • M_2: The total number of containers dispatched from the wrong Dispatch Gate. Specifically, for each Dispatch Gate at (i, N-1), count the number of containers whose numbers, b, do not satisfy N \times i \leq b < N \times (i+1), and the sum of these counts is M_2.
  • M_3: The total number of containers that were not dispatched.

Then, you will obtain the following absolute score. The lower the absolute score, the better.

\[ M_0+10^2M_1+10^4M_2+10^6M_3 \]

For each test case, we compute the relative score \mathrm{round}(10^9\times \frac{\mathrm{MIN}}{\mathrm{YOUR}}), where YOUR is your absolute score and MIN is the lowest absolute score among all competitors obtained on that test case. The score of the submission is the sum of the relative scores.

The final ranking will be determined by the system test with more inputs which will be run after the contest is over. In both the provisional/system test, if your submission produces illegal output or exceeds the time limit for some test cases, only the score for those test cases will be zero, and your submission will be excluded from the MIN calculation for those test cases.

The system test will be performed only for the last submission which received a result other than CE . Be careful not to make a mistake in the final submission.

Number of test cases

  • Provisional test: 50
  • System test: 2000. We will publish seeds.txt (sha256=502228d18e6d0d00f92654ad85f13945fc1d2865f69df352aee061a53ebf3cfa) after the contest is over.

About relative evaluation system

In both the provisional/system test, the standings will be calculated using only the last submission which received a result other than CE. Only the last submissions are used to calculate the MIN for each test case when calculating the relative scores.

The scores shown in the standings are relative, and whenever a new submission arrives, all relative scores are recalculated. On the other hand, the score for each submission shown on the submissions page is the sum of the absolute score for each test case, and the relative scores are not shown. In order to know the relative score of submission other than the latest one in the current standings, you need to resubmit it. If your submission produces illegal output or exceeds the time limit for some test cases, the score shown on the submissions page will be 0, but the standings show the sum of the relative scores for the test cases that were answered correctly.

About execution time

Execution time may vary slightly from run to run. In addition, since system tests simultaneously perform a large number of executions, it has been observed that execution time increases by several percent compared to provisional tests. For these reasons, submissions that are very close to the time limit may result in TLE in the system test. Please measure the execution time in your program to terminate the process, or have enough margin in the execution time.


Input

Input is given from Standard Input in the following format.

N
A_{0,0} \cdots A_{0,N-1}
\vdots
A_{N-1,0} \cdots A_{N-1,N-1}
  • For all test cases, we fix N = 5.
  • A_{i,j} is the number of the j-th container brought in from the Receiving Gate at (i,0), and it is an integer value satisfying 0 \leq A_{i,j} \leq N^2-1. All values of A_{i,j} are distinct.

Output

Output to Standard Output in the following format.

S_0
\vdots
S_{N-1}

Each S_i is a string consisting of the characters P, Q, U, D, L, R, ., and B. The t-th character represents the operation for the crane initially located at (i,0) in the t-th turn. The length of each output string must be between 1 and 10000. The lengths of the strings can be different; in such cases, the shorter strings are padded with . at the end to match the length of the longest string.

Show example

Input Generation

The receiving order A_{i,j} is generated by shuffling the numbers 0, 1, \cdots, N^2-1 in random order and then dividing them into groups of N each.

Tools (Input generator and visualizer)

Please be aware that sharing visualization results or discussing solutions/ideas during the contest is prohibited.


Sample Input 1

5
24 10 17 15 13
14 11 2 1 5
7 9 6 21 20
8 4 19 3 16
18 23 22 0 12

Sample Output 1

PRDDDDRRRQLLLUUPRRRUQ
B
PRQB
PRRRRUUQB
PRRRRQB