/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
出来るだけ少ない種類数のあみだくじを用意して、それらを組み合わせることで全ての並べ替えを作れるようにしてください。
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
正整数 N が与えられます。
正整数 m と m 個の (1,2,\dots,N) の順列を好きに決め、出力してください。このうち i 個目の順列を P_i=(P_{i,1},P_{i,2},\dots,P_{i,N}) とします。
その後、(1,2,\dots,N) の順列 Q=(Q_1,Q_2,\dots,Q_N) が与えられるので、以下の条件を全て満たすような正整数列 A=(A_1,A_2,\dots,A_k) を出力してください。
- 0 \le k \le 2N^2
- A の要素は全て 1 以上 m 以下
- 数列 R=(1,2,\dots,N) に対して、以下の操作を i = 1,2,\dots,k の順に行うと R = Q となる。
- p = P_{A_i} とする。j = 1,2,\dots,N について、R_{j} を R_{p_j} で同時に置き換える。
上記の問題を Q によらずに正解する上での最小の m を m_{\min} とします。あなたの出力する m は m_{\min} でなければなりません。
より厳密には
(1,2,\dots,N) の順列の列 P=(P_1,P_2,\dots,P_m) に対して、以下の条件を満たすとき P を良い順列の列と呼びます。- 任意の (1,2,\dots,N) の順列 Q=(Q_1,Q_2,\dots,Q_N) に対して、以下の条件を全て満たす正整数列 A=(A_1,A_2,\dots,A_k) が存在する。
- 0 \le k \le 2N^2
- A の要素は全て 1 以上 m 以下
- 数列 R=(1,2,\dots,N) に対して、以下の操作を i = 1,2,\dots,k の順に行うと R = Q となる。
- p = P_{A_i} とする。j = 1,2,\dots,N について、R_{j} を R_{p_j} で同時に置き換える。
あなたの出力する P が良い順列の列である必要がないことに注意してください。与えられた Q に対して、条件を満たす A を出力することが出来れば正解と判断されます。
制約
- 3 \le N \le 500
- Q は (1,2,\dots,N) の順列
- 入力は全て整数
入出力
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
まず、ジャッジから正整数 N が以下の形式で与えられます。
N
その後、あなたの選んだ正整数 m と m 個の (1,2,\dots,N) の順列を以下の形式で m+1 行出力してください。ただし、i 個目の順列の j 番目の要素を P_{i,j} とします。出力後、必ず改行してください。
m
P_{1,1}\ P_{1,2}\ \dots\ P_{1,N}
P_{2,1}\ P_{2,2}\ \dots\ P_{2,N}
\vdots
P_{m,1}\ P_{m,2}\ \dots\ P_{m,N}
ただし、以下の条件を満たす必要があります。
- m \le 1000
- (P_{i,1},P_{i,2},\dots,P_{i,N}) は (1,2,\dots,N) の順列
あなたの出力した P が上記の条件を満たさない場合、ジャッジは -1 を出力します。この時、提出はすでに不正解と判定されています。ジャッジプログラムはこの時点で終了するため、あなたのプログラムも終了するのが望ましいです。
その後、ジャッジから (1,2,\dots,N) の順列 Q=(Q_1,Q_2,\dots,Q_N) が以下の形式で与えられます。
Q_1\ Q_2\ \dots\ Q_N
その後、正整数列 A=(A_1,A_2,\dots,A_k) を以下の形式で出力してください。出力後、必ず改行してください。
k\ A_1\ A_2\ \dots\ A_k
以下の条件を満たすとき、またその時に限りジャッジはあなたの出力を正解と判定します。
- m = m_{\min}
- 0 \le k \le 2N^2
- A の要素は全て 1 以上 m 以下
- 数列 R=(1,2,\dots,N) に対して、以下の操作を i = 1,2,\dots,k の順に行うと R = Q となる。
- p = P_{A_i} とする。j = 1,2,\dots,N について、R_{j} を R_{p_j} で同時に置き換える。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
-1を受け取ったらただちにプログラムを終了してください。終了させた場合のジャッジ結果は WA となりますが、終了しなかった場合、ジャッジ結果は不定です。- 解答を出力し終えた時もただちにプログラムを終了してください。終了しなかった場合、ジャッジ結果は不定です。
- 余計な改行は不正なフォーマットの出力とみなされるため、行わないでください。
- この問題のジャッジは適応的 (adaptive) ではありません。ジャッジプログラムは、対話前に Q を決定しています。
入出力例
以下は N=3,Q=(2,3,1) のケースです。
| 入力 | 出力 | 説明 |
|---|---|---|
3 |
N が与えられます。 | |
2
|
m = 2,P_1=(2,1,3),P_2=(3,2,1) を選び、出力します。 | |
2 3 1 |
P が条件を満たしているため、Q=(2,3,1) が与えられます。 | |
2 2 1
|
A=(2,1) は条件を満たします。R は (1,2,3) \rightarrow (3,2,1) \rightarrow (2,3,1) となり、Q に一致します。 また、N = 3 の場合 m_{\min} = 2 であり、m = m_{\min} を満たしています。 よって、この出力は正解と判定されます。 必ず k も含めて 1 行で出力してください。 |
Score : 600 points
Problem Statement
Prepare as few types of ladder lotteries as possible and combine them so that all permutations can be produced.
This is an interactive problem (in which your program and the judge program interact via input and output).
You are given a positive integer N.
Choose any positive integer m and m permutations of (1,2,\dots,N), and output them. Let the i-th permutation be P_i=(P_{i,1},P_{i,2},\dots,P_{i,N}).
Then, a permutation Q=(Q_1,Q_2,\dots,Q_N) of (1,2,\dots,N) is given. Output a sequence of positive integers A=(A_1,A_2,\dots,A_k) satisfying all of the following conditions.
- 0 \le k \le 2N^2
- All elements of A are between 1 and m, inclusive.
- Starting from the sequence R=(1,2,\dots,N), performing the following operation for i = 1,2,\dots,k in order results in R = Q.
- Let p = P_{A_i}. Simultaneously replace R_{j} with R_{p_j} for each j = 1,2,\dots,N.
Let m_{\min} be the minimum value of m for which the above problem can be solved regardless of Q. Your output m must equal m_{\min}.
More precisely
For a sequence of permutations P=(P_1,P_2,\dots,P_m) of (1,2,\dots,N), we call P a good sequence of permutations if the following condition is satisfied:- For any permutation Q=(Q_1,Q_2,\dots,Q_N) of (1,2,\dots,N), there exists a sequence of positive integers A=(A_1,A_2,\dots,A_k) satisfying all of the following conditions.
- 0 \le k \le 2N^2
- All elements of A are between 1 and m, inclusive.
- Starting from the sequence R=(1,2,\dots,N), performing the following operation for i = 1,2,\dots,k in order results in R = Q.
- Let p = P_{A_i}. Simultaneously replace R_{j} with R_{p_j} for each j = 1,2,\dots,N.
Note that your output P does not need to be a good sequence of permutations. Your submission is judged correct if you can output an A satisfying the conditions for the given Q.
Constraints
- 3 \le N \le 500
- Q is a permutation of (1,2,\dots,N).
- All input values are integers.
Input/Output
This is an interactive problem (in which your program and the judge program interact via input and output).
First, the judge gives you a positive integer N in the following format:
N
Then, output your chosen positive integer m and m permutations of (1,2,\dots,N) in the following format over m+1 lines. Here, the j-th element of the i-th permutation is P_{i,j}. Be sure to output a newline at the end.
m
P_{1,1}\ P_{1,2}\ \dots\ P_{1,N}
P_{2,1}\ P_{2,2}\ \dots\ P_{2,N}
\vdots
P_{m,1}\ P_{m,2}\ \dots\ P_{m,N}
The following conditions must be satisfied:
- m \le 1000
- (P_{i,1},P_{i,2},\dots,P_{i,N}) is a permutation of (1,2,\dots,N).
If your output P does not satisfy the above conditions, the judge outputs -1. At that point, your submission has already been judged as incorrect. The judge program terminates at that point, so it is advisable for your program to terminate as well.
Then, the judge gives you a permutation Q=(Q_1,Q_2,\dots,Q_N) of (1,2,\dots,N) in the following format:
Q_1\ Q_2\ \dots\ Q_N
Then, output a sequence of positive integers A=(A_1,A_2,\dots,A_k) in the following format. Be sure to output a newline at the end.
k\ A_1\ A_2\ \dots\ A_k
Your output is judged as correct if and only if the following conditions are all satisfied:
- m = m_{\min}
- 0 \le k \le 2N^2
- All elements of A are between 1 and m, inclusive.
- Starting from the sequence R=(1,2,\dots,N), performing the following operation for i = 1,2,\dots,k in order results in R = Q.
- Let p = P_{A_i}. Simultaneously replace R_{j} with R_{p_j} for each j = 1,2,\dots,N.
Notes
- After each output, insert a newline and flush the standard output. Failure to do so may result in a judge verdict of TLE.
- If you receive
-1, terminate your program immediately. If you do, the judge verdict will be WA, but if you do not, the judge verdict will be indeterminate. - Terminate your program immediately after outputting your answer as well. Otherwise, the judge verdict will be indeterminate.
- Do not output unnecessary newlines, as they will be considered malformatted.
- The judge for this problem is not adaptive. The judge program determines Q before the interaction begins.
Sample Input/Output
The following is a case with N=3,Q=(2,3,1).
| Input | Output | Explanation |
|---|---|---|
3 |
N is given. | |
2
|
Choose m = 2,P_1=(2,1,3),P_2=(3,2,1) and output them. | |
2 3 1 |
Since P satisfies the conditions, Q=(2,3,1) is given. | |
2 2 1
|
A=(2,1) satisfies the conditions. R becomes (1,2,3) \rightarrow (3,2,1) \rightarrow (2,3,1), which matches Q. Also, m_{\min} = 2 for N = 3, and m = m_{\min} is satisfied. Thus, this output is judged as correct. Be sure to output k as well, all on one line. |