A - Polyomino Mining

Time Limit: 3 sec / Memory Limit: 1024 MB

問題文

N\times N マスの島がある。 一番左上のマスの座標を (0,0) とし、そこから下方向に i マス、右方向に j マス進んだ先のマスの座標を (i,j) とする。 占い師の高橋君によると、この島の地下には M 個の油田が存在しているらしい。

各油田は 1\times 1 マスの正方形の辺同士を繋げた連結な ポリオミノ 型をしており、その形状と向きも占いにより事前に判明しているが、島のどこに存在するのかは不明である。 各油田はN\times N マスの範囲内に全体が収まっているが、複数個の油田が重なり、同じマスを共有している可能性がある。 各マス (i,j) に対して、石油埋蔵量 v(i,j) を、そのマスを含む油田の数として定義する。 以下の3種類の操作を最大で 2 N^2 回繰り返す。

  • 1マス選び、そのマスの地下を掘る。v(i,j) の値が明らかになり、1のコストが生じる。
  • 2マス以上の集合 S を選び、それらのマスの石油の埋蔵量の総和 v(S)=\sum_{(i,j)\in S} v(i,j) を占う。選択するマスの数が多いほどコストは低くなり、k マス選んだ場合に生じるコストは 1/\sqrt{k} である。一方で、k に比例して占いによって得られる情報の分散も大きくなる。事前に与えられるパラメータ \epsilon を用いて、以下の平均 \mu と分散 \sigma^2 の正規分布からサンプルされた値を x とする。このとき、情報として得られる値は \max(0,\mathrm{round}(x)) である。
    • 平均 \mu = (k-v(S))\epsilon + v(S)(1-\epsilon)
    • 分散 \sigma^2 = k\epsilon (1-\epsilon)
  • v(i,j)>0 であるようなマスを全て推測する。正解の場合はそこで終了し、不正解の場合はコストを1払って操作を継続する。

出来るだけ少ないコストでv(i,j)>0 であるようなマスを全て特定して欲しい。

得点

v(i,j)>0であるようなマスを指定された操作回数以内に全て特定出来た場合、支払ったコストの総和を C として、\mathrm{round}(10^6\times \max(C, 1/N)) の絶対スコアが得られる。 特定できなかった場合は、10^9 の絶対スコアが得られる。 絶対スコアは小さければ小さいほど良い。

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

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

テストケース数

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

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

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

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

実行時間について

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

入出力

まずはじめに、標準入力から島の大きさ N、油田の個数 M、エラーパラメータ \epsilon、各油田の形状が以下の形式で与えられる。

N M \epsilon
d_1 i_{1,1} j_{1,1} \cdots i_{1,d_1} j_{1,d_1}
\vdots
d_M i_{M,1} j_{M,1} \cdots i_{M,d_M} j_{M,d_M}
  • 島の大きさ N10\leq N\leq 20 を満たす。
  • 油田の個数 M2\leq M\leq 20 を満たす。
  • エラーパラメータ \epsilon0.01\leq \epsilon\leq 0.2 を満たし、0.01 の倍数である。
  • d_kk 番目の油田の面積を表し、4\leq d_k\leq N^2/M を満たす。続く d_k 個の座標 (i_{k,1},j_{k,1}),\cdots,(i_{k,d_k},j_{k,d_k}) は上端と左端の座標が共に 0 となるように平行移動した際のポリオミノに含まれる各マスの座標を表す。例えば、縦2マス、横2マスの正方形型のポリオミノは 4 0 0 0 1 1 0 1 1 と表される。
  • 全く同じ形状の油田が複数含まれる場合もあり得る。

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

各反復では、行う操作の種類に応じて以下の処理を行う。

1マス (i, j) を選び、そのマスの地下を掘る場合、以下の形式で標準出力に出力せよ。

q 1 i j

出力後、v(i,j) の値が標準入力から一行で与えられる。 既に掘ったことのあるマスを再度掘っても構わないが、特に利点はない。

d (\geq 2) マスの集合 S=\{(i_1,j_1),\cdots,(i_d,j_d)\} を選び、v(S) を占う場合、以下の形式で標準出力に出力せよ。

q d i_1 j_1 \cdots i_d j_d

ここで、各座標 (i_k,j_k) は互いに異なる必要がある。同じ座標を複数回含む場合、WA と判定される。 出力後、v(S) の値の 近似値 が標準入力から一行で与えられる。

v(i,j)>0 であるマスを全て推測出来た場合、v(i,j)>0 であると判断したマスの集合を S=\{(i_1,j_1),\cdots,(i_d,j_d)\} とし、以下の形式で標準出力に出力せよ。

a d i_1 j_1 \cdots i_d j_d

ここで、出力する集合 S は1つ目の操作により地下を掘って油田の存在が既に明らかとなったマスも全て含めなければならない。 各座標 (i_k,j_k) は互いに異なる必要がある。同じ座標を複数回含む場合、WA と判定される。 出力後、推測した集合が正しかった場合は 1 が、間違っていた場合は 0 が標準入力から一行で与えられる。 1 が返ってきた場合はその後のクエリは処理されないため、即座に解答プログラムの実行を終了せよ。 0 が返ってきた場合は操作回数の上限まで他の操作を行い、再度挑戦することが出来る。

出力の後には改行をし、更に標準出力を flush しなければならない。 そうしない場合、TLEとなる可能性がある。

解答プログラムは、# から始まるコメント行を出力に含めても良い。 Web版ビジュアライザを使用すると、コメント行を対応するタイミングで表示出来るため、デバッグや考察等に役立てることが出来る。 ジャッジプログラムはコメント行を全て無視するため、コメント行を出力するプログラムをそのまま提出可能である。コメントのうち、#cから開始するものは特別扱いされ、#c i j colorの形式で出力することによりビジュアライザで表示した際に各マスの色を任意に変更できる。例えば #c 0 1 red#c 0 1 #ff0000 と出力すると (0,1) の色が赤に変わる。

t Output Input
事前情報
15 2 0.01
19 0 3 1 1 1 3 1 6 2 0 2 1 2 2 2 3 2 4 2 5 2 6 3 1 3 2 3 3 3 4 3 5 4 1 4 2 4 4
19 0 1 1 1 2 1 3 1 3 2 3 3 3 4 4 1 4 2 5 0 5 1 5 2 5 3 6 1 6 2 6 3 7 1 7 3 8 3
1
q 25 0 0 0 1 0 2 ... 4 4
8
2
q 1 0 0
0
\vdots
185
a 38 2 3 2 10 3 1 ... 10 12
1

例を見る

サンプルプログラム

Pythonでの解答例を示す。 このプログラムでは、全てのマスを順番に掘ってみて実際に石油が出たマスを最後に出力している。

# read prior information
line = input().split()
N = int(line[0])
M = int(line[1])
eps = float(line[2])
fields = []
for _ in range(M):
    line = input().split()
    ps = []
    for i in range(int(line[0])):
        ps.append((int(line[2*i+1]), int(line[2*i+2])))
    fields.append(ps)

# drill every square
has_oil = []
for i in range(N):
    for j in range(N):
        print("q 1 {} {}".format(i, j))
        resp = input()
        if resp != "0":
            has_oil.append((i, j))

print("a {} {}".format(len(has_oil), ' '.join(map(lambda x: "{} {}".format(x[0], x[1]), has_oil))))
resp = input()
assert resp == "1"

入力生成方法

L 以上 U 以下の整数値を一様ランダムに生成する関数を \mathrm{rand}(L,U) で表す。

島の大きさ N\mathrm{rand}(10,20) により生成される。 油田の数 M\mathrm{rand}(2,\mathrm{floor}(N^2/20)) により生成される。 エラーパラメータ \epsilon0.01\times \mathrm{rand}(1,20) により生成される。

各油田の形状を生成するために、まず2つのパラメータ a,d を以下のように生成する。

  • a=\mathrm{floor}(\mathrm{rand}(\mathrm{floor}(N^2/5),\mathrm{floor(N^2/2)})/M)
  • d=\mathrm{rand}(0,a-4)

k=1,\cdots,M に対し、k 番目の油田の形状を以下のように生成する。 まず、油田の大きさを s_k=\mathrm{rand}(a-d,a+d) により生成する。 次に、S_k=\{(\mathrm{floor}(N/2),\mathrm{floor}(N/2))\} から開始し、S_k の大きさが s_k となるまで、S_k に隣接するマス集合の中から一様ランダムに一つ選んで S_k に加えることを繰り返す。 最後に、S_k に含まれるマスの i 座標の最小値と j 座標の最小値がともに 0 となるように平行移動させる。

各油田の形状を生成後、島内の各油田の位置を、島からはみ出さない範囲で一様ランダムに選択する。 すなわち、油田を構成するマスの i 座標の最大値を i_{\text{max}}j 座標の最大値を j_{\text{max}} とすると、 d_i=\mathrm{rand}(0,N-i_{\text{max}}-1)d_j=\mathrm{rand}(0,N-j_{\text{max}}-1) を生成し、(d_i,d_j) だけ平行移動した位置に配置する。

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

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

ツールで用いられる入出力ファイルの仕様

ローカルテスタに与える入力ファイルは解答プログラムに与えられる事前情報の末尾に以下の形式の情報が追加されている。

di_1 dj_1
\vdots
di_M dj_M
v_{0,0} \cdots v_{0,N-1}
\vdots
v_{N-1,0} \cdots v_{N-1,N-1}
e_1
\vdots
e_{2N^2}
  • (di_k,dj_k)k 番目の油田が配置された座標を表す。すなわち、その油田に属するマスのi 座標の最小値が di_kj 座標の最小値が dj_k である。
  • v_{i,j} はマス (i,j) の石油埋蔵量 v(i,j) を表す。
  • e_k は標準正規分布からサンプルされた値であり、k 回目のクエリに対する結果の誤差を計算する際に用いられる。

Problem Statement

There is an island consisting of N\times N squares. 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. According to Mr. Takahashi, the fortune teller, there are M unexplored oil fields under the island.

Each oil field has a connected polyomino shape with the sides of a 1×1 square connected to each other, and its shape and orientation are also known in advance by divination. However, their exact location on the island remains unknown. Each oil field is entirely contained within the N\times N squares, but multiple oil fields may overlap and share the same square. For each (i,j) square, define oil reserves v(i,j) as the number of oil fields containing that square. Repeat the following three types of operations at most 2 N^2 times.

  • Choose one square and drill underground in that square. The value of v(i,j) is revealed, incurring a cost of 1.
  • Choose a set S of two or more squares and divine the sum of the oil reserves in those squares v(S)=\sum_{(i,j)\in S} v(i,j). The larger the number of squares to select, the lower the cost, and selecting k squares costs 1/\sqrt{k}. On the other hand, the variance of the value obtained by divination increases in proportion to k. Let x be the value sampled from the following normal distribution with mean \mu and variance \sigma^2, using an error parameter \epsilon given in advance. Then the obtained value is \max(0,\mathrm{round}(x)).
    • Mean \mu = (k-v(S))\epsilon + v(S)(1-\epsilon)
    • Variance \sigma^2 = k\epsilon (1-\epsilon)
  • Guess all squares such that v(i,j)>0. If the answer is correct, the operation ends there; if it is incorrect, the operation continues at a cost of 1.

Please identify all squares such that v(i,j)>0 with as little cost as possible.

Scoring

If you succeed in identifying all squares such that v(i,j)>0 within the specified number of operations, you obtain an absolute score of \mathrm{round}(10^6\times \max(C, 1/N)), where C is the sum of the costs you paid. If you fail, you will get an absolute score of 10^9. The lower the absolute score, the better.

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: 3000. We will publish seeds.txt (sha256=45933552af3c557ff53d4e1397ef9fe10cedea89f10fe8140ceb489569dacea2) 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 and Output

First, the island size N, the number of oil fields M, the error parameter \epsilon, and the shape of each oil field are given from the Standard Input in the following format.

N M \epsilon
d_1 i_{1,1} j_{1,1} \cdots i_{1,d_1} j_{1,d_1}
\vdots
d_M i_{M,1} j_{M,1} \cdots i_{M,d_M} j_{M,d_M}
  • The island size N satisfies 10\leq N\leq 20.
  • The number of oil fields M satisfies 2\leq M\leq 20.
  • The error parameter \epsilon satisfies 0.01\leq \epsilon\leq 0.2 and is a multiple of 0.01.
  • The d_k denotes the area of the k-th oil field and satisfies 4\leq d_k\leq N^2/M. The subsequent d_k coordinates (i_{k,1},j_{k,1}),\cdots,(i_{k,d_k},j_{k,d_k}) represent the coordinates of each square in the polyomino when translated so that the top and left coordinates are both 0. For example, a 2\times 2 square-shaped polyomino is represented as 4 0 0 0 1 1 0 1 1.
  • It is possible that multiple oil fields may have exactly the same shape.

After reading the above information, repeat the following process at most 2 N^2 times. In each iteration, do the following according to the type of operation to be performed.

If you choose one square (i, j) and drill underground in that square, output to Standard Output in the following format.

q 1 i j

After output, the value of v(i,j) is given in one line from Standard Input. You may drill an already drilled square again, although it has no particular advantage.

If you choose a set S=\{(i_1,j_1),\cdots,(i_d,j_d)\} of d (\geq 2) squares and divine v(S), output to Standard Output in the following format.

q d i_1 j_1 \cdots i_d j_d

Here, each coordinate (i_k,j_k) must be distinct from the others. If the set contains the same coordinate more than once, it is judged as WA. After output, an approximation of the value of v(S) is given in one line from the standard input.

If you can guess all the squares with v(i,j)>0, let S=\{(i_1,j_1),\cdots,(i_d,j_d)\} be the set of squares you determined to have v(i,j)>0 and output it to Standard Output in the following format.

a d i_1 j_1 \cdots i_d j_d

Here, the output set S must include all squares which have already been revealed to have oil reserves by drilling underground through the first operation. Each coordinate (i_k,j_k) must be distinct from the others. If the set contains the same coordinate more than once, it is judged as WA. After the output, 1 is given in one line from the standard input if the guessed set is correct, and 0 if it is wrong. If 1 is returned, the subsequent queries are not processed, and you should immediately terminate the execution of the program. If 0 is returned, you can perform additional operations up to the limit and try again.

The output must be followed by a new line, and you have to flush Standard Output. Otherwise, the submission might be judged as TLE.

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. Among the comments, those starting with #c are treated specially and can be output in the form of #c i j color to let the visualizer change the color of each square arbitrarily. For example, outputting #c 0 1 red or #c 0 1 #ff0000 will change the color of (0,1) to red.

Example

t Output Input
Prior Information
15 2 0.01
19 0 3 1 1 1 3 1 6 2 0 2 1 2 2 2 3 2 4 2 5 2 6 3 1 3 2 3 3 3 4 3 5 4 1 4 2 4 4
19 0 1 1 1 2 1 3 1 3 2 3 3 3 4 4 1 4 2 5 0 5 1 5 2 5 3 6 1 6 2 6 3 7 1 7 3 8 3
1
q 25 0 0 0 1 0 2 ... 4 4
8
2
q 1 0 0
0
\vdots
185
a 38 2 3 2 10 3 1 ... 10 12
1

Show example

Sample Solution

This is a sample solution in Python. This program drills all the squares in order and outputs the squares that actually yielded oil at the end.

# read prior information
line = input().split()
N = int(line[0])
M = int(line[1])
eps = float(line[2])
fields = []
for _ in range(M):
    line = input().split()
    ps = []
    for i in range(int(line[0])):
        ps.append((int(line[2*i+1]), int(line[2*i+2])))
    fields.append(ps)

# drill every square
has_oil = []
for i in range(N):
    for j in range(N):
        print("q 1 {} {}".format(i, j))
        resp = input()
        if resp != "0":
            has_oil.append((i, j))

print("a {} {}".format(len(has_oil), ' '.join(map(lambda x: "{} {}".format(x[0], x[1]), has_oil))))
resp = input()
assert resp == "1"

Input Generation

Let \mathrm{rand}(L,U) be a function that generates a uniform random integer between L and U, inclusive.

The island size N is generated by \mathrm{rand}(10,20). The number of oil fields M is generated by \mathrm{rand}(2,\mathrm{floor}(N^2/20)). The error parameter \epsilon is generated by 0.01\times \mathrm{rand}(1,20).

To generate the shape of each oil field, two parameters a,d are first generated as follows.

  • a=\mathrm{floor}(\mathrm{rand}(\mathrm{floor}(N^2/5),\mathrm{floor(N^2/2)})/M)
  • d=\mathrm{rand}(0,a-4)

For each k=1,\cdots,M, the shape of the k-th oil field is generated as follows. First, the size of the oil field is generated by s_k=\mathrm{rand}(a-d,a+d). Starting with S_k=\{(\mathrm{floor}(N/2),\mathrm{floor}(N/2))\}, add one square at a time, chosen uniformly at random from the set of squares adjacent to S_k, until the size of S_k becomes s_k. Finally, translate S_k so that the minimum i coordinate and the minimum j coordinate of the squares contained in S_k are both 0.

After generating the shape of each oil field, the position of each oil field within the island is chosen uniformly at random, ensuring they do not extend beyond the island's boundaries. In other words, given the maximum i coordinate among the squares composing an oil field as i_{\text{max}}, and the maximum j coordinate as j_{\text{max}}, generate d_i=\mathrm{rand}(0,N-i_{\text{max}}-1) and d_j=\mathrm{rand}(0,N-j_{\text{max}}-1). Then, position the oil field at the location shifted by (d_i, d_j).

Tools (Input generator, local tester, and visualizer)

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

Specification of input/output files used by the tools

The input file provided to the local tester includes additional information in the following format appended to the end of the prior-information given to the solution program.

di_1 dj_1
\vdots
di_M dj_M
v_{0,0} \cdots v_{0,N-1}
\vdots
v_{N-1,0} \cdots v_{N-1,N-1}
e_1
\vdots
e_{2N^2}
  • (di_k,dj_k) denotes the coordinates at which the k-th oil field is located. That is, the minimum i-coordinate of the square belonging to that oil field is di_k and the minimum j-coordinate is dj_k.
  • Each v_{i,j} represents the oil reserves v(i,j) of the square (i,j).
  • Each e_k is a value sampled from the standard normal distribution and is used in calculating the error of the result for the k-th query.
A-Final - Polyomino Mining (System Test)

Time Limit: 0 msec / Memory Limit: 1024 MB