/
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 800 点
問題文
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジが入出力を介して対話を行う形式の問題)です。
1 から 5 までの番号がついた 5 本のキューがあります。 はじめ、全てのキューは空です。
ジャッジから、以下の 2 種類のクエリがそれぞれ Q 回、合計 2Q 回与えられます。
- push クエリ:ジャッジから整数 X が与えられ、キュー 1 の末尾に追加される。各 push クエリで与えられる整数 X は全て相異なる。あなたはその後、以下の移動操作を 0 回以上行うことができる。
- 移動操作:1 \le i,j \le 5 を満たす整数 i,j を選び、キュー i の先頭の値を取り除いてキュー j の末尾に追加する。ただし、空のキューを i として選ぶことはできない。i\ne j である必要はない。
- pop クエリ:ジャッジは、1 本以上のキューが空でない状態でのみこのクエリを与える。あなたは 1 \le i \le 5 を満たす整数 i を選ぶ。このとき、キュー i は空でなく、その先頭の値が、その時点で 5 本のキューに入っている全ての値のうち最小でなければならない。その後、ジャッジがキュー i の先頭の値を取り除く。
移動操作は合計で 10^5 回までしか行えません。 全てのクエリに正しく応答するプログラムを作成してください。
制約
- 1 \le Q \le 5000
- 1 \le X \le 10^9
- 各 push クエリで追加される値 X は全て相異なる
- push クエリと pop クエリはそれぞれ Q 回ずつ与えられる
- 各 pop クエリが与えられる時点で、1 本以上のキューが空でない
- 入力される値は全て整数
入出力
この問題はインタラクティブな問題です。
まず、ジャッジから正整数 Q が以下の形式で与えられます。
Q
その後、以下のやり取りを 2Q 回行ってください。 各回では、まずジャッジから push クエリまたは pop クエリが与えられます。
push クエリは以下の形式で与えられます。
1 X
pop クエリは以下の形式で与えられます。
2
(なお、直前のクエリへの応答が不正であった場合、クエリの代わりに -1 が標準入力から与えられます。この場合はただちにプログラムを正常終了してください。ただし、2Q 個目のクエリへの応答で初めて不正な出力を行った場合、その後の入力は何も与えられません。)
push クエリが与えられた場合、行う移動操作の回数を k とし、r 回目の移動操作で選ぶキューの番号を i_r,j_r として、以下の形式で k+1 行出力してください。出力後、必ず改行してください。
k i_1 j_1 i_2 j_2 \vdots i_k j_k
pop クエリが与えられた場合、先頭の値を取り除くキューの番号 i を以下の形式で出力してください。出力後、必ず改行してください。
i
注意点
- 各クエリへの応答を出力するたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- ジャッジから
-1という入力を受け取った場合は、ただちにプログラムを正常終了してください。終了した場合のジャッジ結果は WA となりますが、終了しなかった場合のジャッジ結果は不定です。 - 余分な改行は不正な形式の出力とみなされるため、行わないでください。
- 2Q 個のクエリへの応答を終えたら、ただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
- この問題のジャッジは適応的(adaptive)ではありません。ジャッジは、対話前にクエリの種類と順序、および各 push クエリで与える整数 X を決定しています。
入出力例
以下は Q=2 で、ジャッジが 5,3 の順に値を追加するケースです。
| 入力 | 出力 | 説明 |
|---|---|---|
2 |
Q が与えられます。 | |
1 5 |
push クエリです。X=5 が与えられ、キュー 1 の末尾に追加されます。 | |
0 |
移動操作を行う回数は 0 回です。 | |
1 3 |
push クエリです。X=3 が与えられ、キュー 1 の末尾に追加されます。 | |
3 1 2 1 2 2 2 |
移動操作を 3 回行います。5 をキュー 2 へ移動し、3 をキュー 2 へ移動し、キュー 2 の先頭の 5 を同じキューの末尾へ移動します。これにより、キュー 2 は先頭から 3,5 の順になります。 | |
2 |
pop クエリです。 | |
2 |
キュー 2 を選びます。その後、ジャッジがキュー 2 の先頭の 3 を取り除きます。 | |
2 |
pop クエリです。 | |
2 |
キュー 2 を選びます。その後、ジャッジがキュー 2 の先頭の 5 を取り除きます。 |
Score : 800 points
Problem Statement
This is an interactive problem (in which your program interacts with the judge via input and output).
There are five queues numbered 1 through 5. Initially, all queues are empty.
The judge gives you the following two types of queries, Q times each, for a total of 2Q times.
- push query: The judge gives you an integer X, which is appended to the back of queue 1. The integers X given in the push queries are all distinct. Then, you may perform the following move operation zero or more times.
- move operation: Choose integers i,j satisfying 1 \le i,j \le 5, remove the value at the front of queue i, and append it to the back of queue j. Here, you cannot choose an empty queue as i. It is not required that i\ne j.
- pop query: The judge gives this query only when at least one queue is non-empty. You choose an integer i satisfying 1 \le i \le 5. At this point, queue i must be non-empty, and the value at its front must be the minimum among all the values currently contained in the five queues. Then, the judge removes the value at the front of queue i.
You may perform the move operation at most 10^5 times in total. Create a program that correctly responds to all queries.
Constraints
- 1 \le Q \le 5000
- 1 \le X \le 10^9
- The values X appended in the push queries are all distinct.
- The push query and the pop query are each given Q times.
- At the time each pop query is given, at least one queue is non-empty.
- All input values are integers.
Interaction
This problem is interactive.
First, the judge gives a positive integer Q in the following format:
Q
Then, perform the following interaction 2Q times. In each round, the judge first gives a push query or a pop query.
A push query is given in the following format:
1 X
A pop query is given in the following format:
2
(If your response to the previous query was invalid, -1 will be given from Standard Input instead of a query. In this case, immediately terminate your program normally. However, if your first invalid output is your response to the 2Q-th query, no further input will be given after that.)
If a push query is given, let k be the number of move operations you perform, and let i_r,j_r be the queue numbers chosen in the r-th move operation; output k+1 lines in the following format. Be sure to add a newline at the end of the output.
k i_1 j_1 i_2 j_2 \vdots i_k j_k
If a pop query is given, output the number i of the queue from which to remove the front value, in the following format. Be sure to add a newline at the end of the output.
i
Notes
- Each time you output a response to a query, flush Standard Output after adding a newline at the end. Otherwise, the judge result may be TLE.
- If you receive
-1as input from the judge, immediately terminate your program normally. If you do so, the judge result will be WA; otherwise, the judge result is indeterminate. - Extra newlines are regarded as malformed output, so do not output them.
- After you finish responding to the 2Q queries, immediately terminate your program. Otherwise, the judge result will be indeterminate.
- The judge for this problem is not adaptive. Before the interaction begins, the judge determines the types and order of the queries, and the integer X given in each push query.
Sample Interaction
Below is a case where Q=2, and the judge appends the values 5,3 in this order.
| Input | Output | Description |
|---|---|---|
2 |
Q is given. | |
1 5 |
A push query. X=5 is given, and it is appended to the back of queue 1. | |
0 |
Perform zero move operations. | |
1 3 |
A push query. X=3 is given, and it is appended to the back of queue 1. | |
3 1 2 1 2 2 2 |
Perform three move operations. Move 5 to queue 2, move 3 to queue 2, and move the front value 5 of queue 2 to the back of the same queue. As a result, queue 2 contains 3,5 from front to back. | |
2 |
A pop query. | |
2 |
Choose queue 2. Then, the judge removes the front value 3 of queue 2. | |
2 |
A pop query. | |
2 |
Choose queue 2. Then, the judge removes the front value 5 of queue 2. |