Time Limit: 2 sec / Memory Limit: 1024 MiB
ストーリー
高橋店長は、ロボットが調理を担当する喫茶店「F喫茶」を開業することにした。 店で働く調理用のロボットは、バーコードを見せると、それに対応する商品を作ってくれる。 F喫茶では多数の商品を提供する予定であり、高橋店長は各商品のバーコードを一枚ずつ紙に印刷しておくつもりだった。 ところが開店当日になって、全ての商品のバーコードを印刷するには紙の枚数が足りないことに気がついた。
そこで高橋店長は、白・黒の模様を印刷し、ところどころに穴を開けた紙を重ね合わせることを思いついた。 重ね順を変えて様々なバーコードを表示できれば、商品数より少ない枚数の紙で全種類に対応できるはずである。 紙は専用の装置に重ねて入れ、正面に露出している部分をつまんで引き出し、好きな位置に差し戻すことができる。
今日提供する商品と、それぞれに対応するバーコードは決まっているが、注文が入る順番は分からない。 高橋店長は、開店前に紙の模様を工夫しておき、営業中の注文ミスと紙の入れ替えをなるべく少なくしたいと考えている。 限られた枚数の紙の設計と操作を工夫して、次々に届く注文を手際よくロボットに伝えてほしい。
小さなケースでの紙の重ね順とバーコードの変化
問題文
N 個の窓が横一列に並んだバーコード表示装置と、K 枚の紙がある。 窓には 0 から N-1、紙には 0 から K-1 の番号が付いている。 各紙には各窓に対応する位置があり、全ての紙を同じ位置に重ねてバーコードを表示する。
商品は P 種類あり、それぞれに対応するバーコードの一覧 A_0,\ldots,A_{P-1} が最初に与えられる。
各バーコードは互いに異なる長さ N の 0 と 1 からなる文字列である。
この一覧を見てから、紙 k の模様を長さ N の文字列 S_k として設計する。
各文字は白を表す 0、黒を表す 1、穴を表す . のいずれかである。
全て穴の紙も使用できる。
設計後に模様を変更することはできず、初期の重ね順は上から紙 0,1,\ldots,K-1 である。
窓 j には、その位置が穴でない最も上の紙の色が表示される。 そのような紙がなければ白が表示される。 この最も上の紙を「窓 j で見えている紙」と呼ぶ。
設計を提出した後、R 回の注文が一つずつ順に与えられる。 注文 r では商品の番号 a_r が与えられるので、以下の操作を繰り返すことで、表示を A_{a_r} に一致させることを目指す。
- 少なくとも1つの窓で見えている紙 k と、整数 d を選ぶ。 紙 k を取り出し、残りの紙のうち上から d 枚の下へ差し込む。 ここで 0\leq k<K、0\leq d<K とする。 d=0 なら一番上、d=K-1 なら一番下となり、他の紙どうしの順序は保たれる。
操作列を実行した後の表示が A_{a_r} と一致しない場合、その注文を誤りとして数える。 不一致の窓の数によらず、誤りは1件の注文につき1回と数え、表示が一致しているかどうかにかかわらず次の注文へ進む。 次の注文は現在の注文を処理するまで知らされず、注文の間も重ね順を引き継ぐ。 各注文での操作回数を 2K 回以下に抑えつつ全ての注文を処理し、以下に定義する絶対スコアをできるだけ小さくせよ。
得点
誤りとなった注文の数を E、操作の総回数を T とする。 全ての注文を処理したとき、以下の絶対スコアが得られる。
\[ 2KE+T \]
絶対スコアは小さいほど良い。
各テストケース毎に、絶対スコアの小さい順での順位に応じた順位スコアが得られ、その和が提出の得点となる。 順位スコアは以下のように計算され、高ければ高いほど良い。
コンテストの提出者数を n_{submit}、自身より小さい絶対スコアを獲得した参加者の人数を n_{lose}、自身と等しい絶対スコアを獲得した他の参加者の人数を n_{tie} とする。この時、このテストケースにおける順位は r=n_{lose}+0.5 n_{tie} と定まり、順位スコアは \mathrm{round}(10^9\times (1-\frac{r}{n_{submit}})) となる。
最終順位はコンテスト終了後に実施されるより多くの入力に対するシステムテストにおける得点で決定される。 暫定テスト、システムテストともに、不正な出力や制限時間超過をした場合、そのテストケースの順位スコアのみ 0 点となる。 システムテストは CE 以外の結果を得た一番最後の提出に対してのみ行われるため、最終的に提出する解答を間違えないよう注意せよ。
テストケース数
- 暫定テスト: 50 個
- システムテスト: 2000 個、コンテスト終了後に seeds.txt (sha256=9562a1597447a1116b5b4b869d74b3598ce13bc33b1ed8bd9ac1ee9df33d92e7) を公開
相対評価システムについて
暫定テスト、システムテストともに、 CE 以外の結果を得た一番最後の提出のみが順位表に反映される。
順位表に表示されているスコアは相対評価スコアであり、新規提出があるたびに、相対評価スコアが再計算される。 一方、提出一覧から確認できる各提出のスコアは各テストケースごとの絶対スコアをそのまま足し合わせたものであり、相対評価スコアは表示されない。 最新以外の提出について、現在の順位表における相対評価スコアを知るためには、再提出が必要である。 不正な出力や制限時間超過をした場合、提出一覧から確認できるスコアは 0 となるが、順位表には正解したテストケースに対する順位スコアの和が表示される。
実行時間について
実行時間には多少のブレが生じる。 また、システムテストでは同時に大量の実行を行うため、暫定テストに比べて数%程度実行時間が伸びる現象が確認されている。 そのため、実行時間制限ギリギリの提出がシステムテストで TLE となる可能性がある。 プログラム内で時間を計測して処理を打ち切るか、実行時間に余裕を持たせることを推奨する。
入出力
本問題は reactive形式の問題である。 最初に以下の事前情報が標準入力から与えられる。
N K P R
A_0
\vdots
A_{P-1}
全てのケースで N=64、K=192、P=256、R=1024 である。
各 A_v は 1 をちょうど 32 個含む。
まず以下の形式で紙の設計を標準出力へ出力する。
S_0
\vdots
S_{K-1}
その後、以下のクエリを R 回処理せよ。 r 回目のクエリでは、注文する商品の番号 a_r が以下の形式で標準入力から与えられる。
a_r
操作回数を m、i 回目の操作で選ぶ紙と挿入位置をそれぞれ k_i,d_i とし、以下の形式で標準出力へ出力せよ。
m
k_0 d_0
\vdots
k_{m-1} d_{m-1}
ここで 0\leq m\leq 2K とする。
操作を行わない場合は 0 のみを出力する。
各操作を順に行った後、表示が A_{a_r} と一致しない場合は誤り回数が1増える。
最後の注文への操作列を出力したら、プログラムを終了せよ。
最後の注文が正常に処理された場合、ジャッジからの入力はない。
不正な設計、不正な操作や出力形式が検出された場合は -1 が与えられ、解答は不正と判定される。
-1 を受け取ったら直ちにプログラムを終了せよ。
全ての注文を処理する前の出力終了や、対話終了後の余分な出力も不正と判定される。
各出力の末尾で改行し、さらに標準出力を flush しなければならない。 そうしない場合、TLEとなる可能性がある。
例
説明のため、実際の制約よりも小さな N=4,K=3,P=4,R=3 の例を示す。 ストーリー末尾のアニメーションは、この例の紙の重ね順と表示の変化を示している。
| t | Input | Output |
|---|---|---|
| 事前情報 | 4 3 4 3 1001 1100 0011 0110 | |
| 設計 | 10.1 1100 0.1. | |
| 0 | 0 | 0 |
| 1 | 1 | 1 0 2 |
| 2 | 2 | 1 1 2 |
最初の表示は 1001 なので、注文 0 では操作を行わない。
注文 1 では紙 0 を一番下へ移して 1100 を表示し、注文 2 では紙 1 を一番下へ移して 0011 を表示する。
サンプルプログラム (Python)
import sys
N, K, P, R = map(int, input().split())
patterns = [input().strip() for _ in range(P)]
for k in range(K):
print(patterns[k])
sys.stdout.flush()
top = 0
for _ in range(R):
request = int(input())
if request == -1:
sys.exit()
count = (request - top) % K if request < K else 0
print(count)
for _ in range(count):
print(top, K - 1)
top = (top + 1) % K
sys.stdout.flush()
入力生成方法
1 をちょうど 32 個含む長さ 64 の全ての 01 文字列から、P 個を一様に非復元抽出し、抽出順に A_0,\ldots,A_{P-1} とする。
a_0 は 0,\ldots,P-1 から一様に選ぶ。
r\geq 1 では、a_{r-1} 以外の P-1 個の番号から a_r を一様に選ぶ。
ツール(入力ジェネレータ・ローカルテスタ・ビジュアライザ)
- Web版: ローカル版より高性能でアニメーション表示が可能です。
- ローカル版: 使用するにはRust言語のコンパイル環境をご用意下さい。
コンパイルが通らない場合は
rustup updateをお試し下さい。- Windows用のコンパイル済みバイナリ: Rust言語の環境構築が面倒な方は代わりにこちらをご利用下さい。
コンテスト期間中に、ビジュアライズ結果の共有や、解法・考察に関する言及は禁止されています。 ご注意下さい。
ツールで用いられる入力ファイルの仕様
ローカルテスタに与える入力ファイルは、事前情報の末尾に以下の形式の情報が追加されている。
a_0
\vdots
a_{R-1}
- a_r は注文 r の番号である。
Story
Takahashi has decided to open F Cafe, a cafe where robots prepare the food and drinks. When shown a barcode, a kitchen robot makes the corresponding item. With many items on the menu, Takahashi planned to print each item's barcode on a separate sheet of paper. On opening day, however, he realized that he did not have enough sheets to print them all.
Takahashi came up with an idea: stack sheets printed with black-and-white patterns and cut holes in some places. By rearranging the sheets to display different barcodes, he should be able to handle every item using fewer sheets than there are items. The sheets are stacked in a special device, where a sheet can be pulled out by a part exposed at the front and reinserted at any position.
The items available today and their barcodes are known, but the order in which customers will request them is not. Takahashi wants to design the sheets before opening so that he can minimize both incorrect orders and sheet movements during business hours. Help him design and rearrange the limited number of sheets to efficiently convey incoming orders to the robots.
Changes in the sheet order and displayed barcode in a small example
Problem Statement
There is a barcode display device with N windows arranged in a horizontal row, and there are K sheets of paper. The windows are numbered from 0 to N-1, and the sheets from 0 to K-1. Each sheet has a position corresponding to each window, and all sheets are stacked with these positions aligned to display a barcode.
There are P types of items, and the list of their corresponding barcodes A_0,\ldots,A_{P-1} is given at the beginning.
The barcodes are distinct strings of length N consisting of 0 and 1.
After receiving this list, you design the pattern of sheet k as a string S_k of length N.
Each character is either 0 for white, 1 for black, or . for a hole.
A sheet consisting entirely of holes is also allowed.
The patterns cannot be changed after they are designed, and the initial order of the sheets, from top to bottom, is 0,1,\ldots,K-1.
Window j displays the color of the topmost sheet that does not have a hole at that position. If there is no such sheet, the window displays white. This topmost sheet is called the "sheet visible through window j."
After you submit the designs, R orders are given one at a time. For order r, you are given the item number a_r and aim to make the displayed barcode match A_{a_r} by repeating the following operation.
- Choose a sheet k that is visible through at least one window and an integer d. Remove sheet k and reinsert it below the top d sheets among the remaining sheets. Here, 0\leq k<K and 0\leq d<K. If d=0, the sheet is placed at the top; if d=K-1, it is placed at the bottom. The relative order of all other sheets is preserved.
If the displayed barcode after the sequence of operations does not match A_{a_r}, the order is counted as incorrect. Each incorrect order counts as one error regardless of the number of mismatched windows, and you proceed to the next order whether or not the displayed barcode matches. The next order is not revealed until the current order has been processed, and the sheet order is carried over between orders. Process all orders using at most 2K operations per order, and minimize the absolute score defined below.
Scoring
Let E be the number of incorrect orders and T be the total number of operations. After processing all orders, you obtain the following absolute score:
\[ 2KE+T \]
The lower the absolute score, the better.
For each test case, you will obtain a rank score according to your rank determined by lower absolute score. The score of the submission is the total rank score for each test case. The rank score is calculated as follows, and the higher the rank score, the better.
Let n_{submit} be the number of contestants with submissions, n_{lose} be the number of contestants who received an absolute score lower than yours, and n_{tie} be the number of other contestants who received an absolute score equal to yours. Then your rank in this test case is determined as r=n_{lose}+0.5 n_{tie}, and your rank score is \mathrm{round}(10^9\times (1-\frac{r}{n_{submit}})).
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 rank score for those test cases will be zero. 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=9562a1597447a1116b5b4b869d74b3598ce13bc33b1ed8bd9ac1ee9df33d92e7) 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 .
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 rank 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
This is a reactive problem. First, the following initial information is given from Standard Input:
N K P R
A_0
\vdots
A_{P-1}
In all test cases, N=64, K=192, P=256, and R=1024.
Each A_v contains exactly 32 occurrences of 1.
First, output the sheet designs to Standard Output in the following format:
S_0
\vdots
S_{K-1}
Then, process the following query R times. In the r-th query, the item number a_r is given from Standard Input in the following format:
a_r
Let m be the number of operations, and let k_i and d_i be the sheet and insertion position chosen for the i-th operation, respectively. Output them to Standard Output in the following format:
m
k_0 d_0
\vdots
k_{m-1} d_{m-1}
Here, 0\leq m\leq 2K.
If you perform no operations, output only 0.
After the operations are performed in order, the error count increases by one if the displayed barcode does not match A_{a_r}.
Terminate your program after outputting the sequence of operations for the last order.
If the last order is processed normally, no further input is given by the judge.
If an invalid design, operation, or output format is detected, -1 is given and your solution is judged as invalid.
Terminate your program immediately upon receiving -1.
Ending your output before processing all orders or producing extra output after the interaction ends is also judged as invalid.
Each output must be followed by a newline, and you must flush standard output. Otherwise, the submission may be judged as TLE.
Example
For illustration, consider a small example with N=4,K=3,P=4,R=3, which is smaller than the actual constraints. The animation at the end of the story shows the changes in the sheet order and display for this example.
| t | Input | Output |
|---|---|---|
| Initial information | 4 3 4 3 1001 1100 0011 0110 | |
| Designs | 10.1 1100 0.1. | |
| 0 | 0 | 0 |
| 1 | 1 | 1 0 2 |
| 2 | 2 | 1 1 2 |
The initial display is 1001, so no operations are performed for order 0.
For order 1, sheet 0 is moved to the bottom to display 1100; for order 2, sheet 1 is moved to the bottom to display 0011.
Sample Program (Python)
import sys
N, K, P, R = map(int, input().split())
patterns = [input().strip() for _ in range(P)]
for k in range(K):
print(patterns[k])
sys.stdout.flush()
top = 0
for _ in range(R):
request = int(input())
if request == -1:
sys.exit()
count = (request - top) % K if request < K else 0
print(count)
for _ in range(count):
print(top, K - 1)
top = (top + 1) % K
sys.stdout.flush()
Input Generation
Sample P strings uniformly without replacement from all strings of length 64 consisting of 0 and 1 with exactly 32 occurrences of 1, and assign them to A_0,\ldots,A_{P-1} in the order sampled.
Choose a_0 uniformly from 0,\ldots,P-1.
For r\geq 1, choose a_r uniformly from the P-1 numbers other than a_{r-1}.
Tools (Input generator, local tester, and visualizer)
- Web version: This is more powerful than the local version providing animations.
- Local version: You need a compilation environment of Rust language.
If it does not compile, please try
rustup update.- Pre-compiled binary for Windows: If you are not familiar with the Rust language environment, please use this instead.
Please be aware that sharing visualization results or discussing solutions/ideas during the contest is prohibited.
Specification of the input file used by the tools
The input file given to the local tester contains the following information appended to the initial information:
a_0
\vdots
a_{R-1}
- a_r is the item number for order r.
Time Limit: 2 sec / Memory Limit: 1024 MiB