A - Transit Warehouse Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

ストーリー

高橋くんはコンテナを輸送する中継倉庫の管理をしている。 この倉庫には本日いくつかのコンテナが運び込まれる予定で、運び込まれた全てのコンテナは翌日決められた順序でトラックに積まれて運び出される。 各コンテナが到着する順番は不明であり、運び出す順序を考慮して臨機応変に倉庫内の適切な場所に格納して欲しい。

問題文

D\times D マスの倉庫がある。 一番北西のマスの座標を (0,0) とし、そこから南方向に i マス、東方向に j マス進んだ先のマスの座標を (i,j) とする。 D は奇数であり、(0,(D-1)/2) のマスに倉庫の出入り口がある。 出入り口とその隣接する 3 マスを除いた D^2-4 マスのうち、N マスに通行不能な障害物が置かれている。

D^2-1-N 個のコンテナが一つずつに運び込まれて来る。 各コンテナには 0 から D^2-2-N の固有の番号が振られており、各番号はそのコンテナを運び出したい順序を示している。 コンテナが運び込まれて来る順番の情報は事前には与えられず、運び込まれて来たタイミングでそのコンテナに振られている番号が明らかになる。 出入り口と障害物マス以外の D\times D-1-N マスにはそれぞれ高々 1 つのコンテナを格納することが出来る。 コンテナが1つ運び込まれて来る度に、そのコンテナを格納する場所を選択せよ。 格納先は以下の条件を満たす必要がある。

  1. 出入り口と障害物マス以外であり、かつ既にその場所に他のコンテナが格納されていない。
  2. 出入り口のマスから、4方向に隣接する空きマスのみを経由して格納先のマスに到達可能である。

コンテナの格納が全て完了した後、全てのコンテナを任意の順番で1つずつ運び出す。 運び出すコンテナは以下の条件を満たす必要がある。

  1. 出入り口のマスから、4方向に隣接する空きマスのみを経由して運び出すコンテナが格納されているマスに到達可能である。

出来るだけ指定された順序で運び出すことが出来るように、コンテナの格納先と運び出す順序を最適化せよ。

得点

i (0\leq i\leq D^2-2-N) 番目に運び出したコンテナの番号を b_i (0\leq b_i\leq D^2-2-N) とする。 Bb_0,\cdots,b_{D^2-2-N} の転倒数、すなわち、i < j であって、b_i>b_j であるようなペア (i,j) の総数とする。 このとき、以下の得点が得られる。

\[ \mathrm{round}\left(10^9\times \frac{(D^2-N)(D^2-1-N)/2-B}{(D^2-N)(D^2-1-N)/2}\right) \]

N=0,1,\cdots,9 に対して 30 個ずつ、合計で 300 個のテストケースがあり、各テストケースの得点の合計が提出の得点となる。 一つ以上のテストケースで不正な出力や制限時間超過をした場合、提出全体の判定がWATLEとなる。 コンテスト時間中に得た最高得点で最終順位が決定され、コンテスト終了後のシステムテストは行われない。 同じ得点を複数の参加者が得た場合、提出時刻に関わらず同じ順位となる。

入出力

まずはじめに、標準入力から倉庫の大きさ D、障害物の個数 N、障害物の座標 (ri_0,rj_0),\cdots,(ri_{N-1},rj_{N-1}) が以下の形式で与えられる。

D N
ri_0 rj_0
\vdots
ri_{N-1} rj_{N-1}

全てのテストケースで D = 9 で固定である。 N0\leq N\leq D を満たす。 各障害物 (ri_k,rj_k) の座標は出入り口とその隣接する3マスのいずれとも異なり、更に障害物以外の全てのマスは出入り口のマスから到達可能であることが保証されている。

上記の情報を読み込んだら、以下の処理を D^2-1-N 回繰り返す。

d(0\leq d\leq D^2-2-N) 回目の処理ではまず、標準入力から d 番目に運び込まれて来たコンテナの番号 t_d が一行で与えられる。 各 t_d0 以上 D^2-2-N 以下の整数値であり、各コンテナの番号は一意である。

t_d の情報を読み込んだら、そのコンテナを格納する先のマスの座標 (pi_d,pj_d) を以下の形式で一行で標準出力に出力せよ。

pi_d pj_d

出力の後には改行をし、更に標準出力を flush しなければならない。 そうしない場合、TLEとなる可能性がある。 d 回目の出力するまで、d+1 個目のコンテナに関する入力は与えられないので注意せよ。

全てのコンテナの格納が完了後、コンテナを運び出す順序を決定せよ。 k 番目に運び出すコンテナの座標を (qi_k, qj_k) としたとき、以下の形式で標準出力に出力せよ。

qi_0 qj_0
qi_1 qj_1
\vdots
qi_{D^2-2-N} qj_{D^2-2-N}

解答プログラムは、# から始まるコメント行を出力に含めても良い。 Web版ビジュアライザを使用すると、コメント行を対応するタイミングで表示出来るため、デバッグや考察等に役立てることが出来る。 ジャッジプログラムはコメント行を全て無視するため、コメント行を出力するプログラムをそのまま提出可能である。

d Input Output
事前情報
9 0
0
16
8 0
1
13
8 8
\vdots
79
34
0 5
運び出す順序
0 3
0 5
0 2
\vdots
8 8

例を見る

入力生成方法

障害物の個数 N0 以上 D 以下の整数から一様ランダムに生成される。 各障害物の座標は、出入り口とその隣接マス以外の中から異なる N 個がランダムに生成され、残りのマスのうちで到達不能なものがある場合は障害物の座標の生成をやり直す。 コンテナの番号 t_0,\cdots,t_{D^2-2-N}0,1,\cdots,D^2-2-N をランダムな順に並び替えることで生成される。

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

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

Story

Takahashi manages a transit warehouse for transporting containers. Today, many containers are scheduled to be brought into this warehouse, and tomorrow, all of these containers will be loaded onto trucks in a predetermined order for transportation. The order in which the containers will arrive is unknown, so please flexibly store them in appropriate locations in the warehouse, taking into account the order in which they will be transported out.

Problem Statement

There is a warehouse with a grid of D\times D squares. Let (0,0) be the coordinates of the northwesternmost square and (i,j) be the coordinates of the squares located i squares to the south and j squares to the east from there. The warehouse entrance is in the square (0,(D-1)/2), where D is an odd number. There are impassable obstacles placed in N squares out the D^2-4 squares except for the entrance and its 3 adjacent squares.

D^2-1-N containers are brought into the warehouse one by one. Each container is assigned a unique number from 0 to D^2-2-N which indicates the order in which the containers are to be transported out. Information on the order of containers is not given in advance, and the number assigned to a container is revealed at the time it is brought in. Each D\times D-1-N square other than the entrance and obstacles can hold at most 1 container. Each time a container is brought in, choose a square to store it. The chosen square must satisfy the following conditions.

  1. It is neither the entrance nor an obstacle square, and there are no other containers stored at the chosen square.
  2. It must be reachable from the entrance by only passing through adjacent empty squares in the four directions.

After all the containers are stored, please transport out all of them one by one. The container to be transported out must satisfy the following conditions.

  1. The square containing the container to be transported out must be reachable from the entrance by only passing through adjacent empty squares in the four directions.

Optimize the storage location of the containers and the order in which they are transported out so that the order is as close as possible to the specified order.

Scoring

Let b_i (0\leq b_i\leq D^2-2-N) be the number assigned to the container transported out for the i-th time (0\leq i\leq D^2-2-N). Let B be the number of inversions of b_0,\cdots,b_{D^2-2-N}, that is, the total number of pairs (i,j) such that i < j and b_i>b_j. Then, you will obtain the following score.

\[ \mathrm{round}\left(10^9\times \frac{(D^2-N)(D^2-1-N)/2-B}{(D^2-N)(D^2-1-N)/2}\right) \]

There are 30 test cases for each N=0,1,\cdots,9, for a total of 300 test cases, and the score of a submission is the total score for each test case. If your submission produces an illegal output or exceeds the time limit for some test cases, the submission itself will be judged as WA or TLE , and the score of the submission will be zero. The highest score obtained during the contest will determine the final ranking, and there will be no system test after the contest. If more than one participant gets the same score, they will be ranked in the same place regardless of the submission time.

Input and Output

First, the size of the warehouse, D, the number of obstacles N, and the coordinates (ri_0,rj_0),\cdots,(ri_{N-1},rj_{N-1}) of obstacles are given from Standard Input in the following format.

D N
ri_0 rj_0
\vdots
ri_{N-1} rj_{N-1}

For all test cases, we fix D = 9. The number of obstacles N satisfies 0\leq N\leq D. The coordinates of each obstacle (ri_k,rj_k) are different from the entrance and any of its three adjacent squares, furthermore, all squares except the obstacles are guaranteed to be reachable from the entrance.

After reading the above information, repeat the following process D^2-1-N times.

In the d-th process (0\leq d\leq D^2-2-N), the number t_d assigned to the d-th container brought in is given on a single line from Standard Input. Each t_d is an integer value between 0 and D^2-2-N, and each container has a unique number.

After reading the information of t_d, output the coordinates (pi_d,pj_d) of the square to store the container to Standard Output on a single line in the following format.

pi_d pj_d

The output must be followed by a new line, and you have to flush Standard Output. Otherwise, the submission might be judged as TLE. Note that the input for the d+1-th container will not be given until the d-th output is given.

After all containers have been stored, determine the order in which the containers are to be transported out. Let (qi_k, qj_k) be the coordinates of the k-th container to be transported out. Then, output to Standard Output in the following format.

qi_0 qj_0
qi_1 qj_1
\vdots
qi_{D^2-2-N} qj_{D^2-2-N}

Your program may output comment lines starting with #. The web version of the visualizer displays the comment lines at the corresponding timing, which may be useful for debugging and analysis. Since the judge program ignores all comment lines, you can submit a program that outputs comment lines as is.

d Input Output
Prior information
9 0
0
16
8 0
1
13
8 8
\vdots
79
34
0 5
Transporting order
0 3
0 5
0 2
\vdots
8 8

Show example

Input Generation

The number of obstacles N is generated uniformly at random from integers between 0 and D, inclusive. A distinct set of N coordinates for the obstacles are randomly chosen from squares other than the entrance and its three adjacent squares. The numbers t_0,\cdots,t_{D^2-2-N} assigned to the containers are generated by randomly shuffling 0,1,\cdots,D^2-2-N.

Tools (Input generator and visualizer)

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