AtCoder Regular Contest 218
C
梯子抽签
Amidakuji
AtCoder
⌀ 1826
原题
← BD →

梯子抽签AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

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 $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$. It is guaranteed that at least one good sequence of permutations exists. Thus, the minimum value $m_{\min}$ of the length $m$ of a good sequence of permutations is determined. Your output $m$ must equal $m_{\min}$. 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$.

准备尽可能少种类的梯子抽签,并将它们组合起来,使得所有排列都能够生成。

这是一个交互题(你的程序会通过输入输出与评测程序进行交互)。

给定一个正整数 $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$。

设无论 $Q$ 取何值,上述问题都能解决时 $m$ 的最小值为 $m_{\min}$。你的输出 $m$ 必须等于 $m_{\min}$。

更严格地说,对于 $(1,2,\dots,N)$ 的排列序列 $P=(P_1,P_2,\dots,P_m)$,如果对任意 $(1,2,\dots,N)$ 的排列 $Q=(Q_1,Q_2,\dots,Q_N)$,都存在满足以下所有条件的正整数序列 $A=(A_1,A_2,\dots,A_k)$,则称 $P$ 为良好的排列序列。

  • $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}$。

保证至少存在一个良好的排列序列。因此,良好的排列序列长度 $m$ 的最小值 $m_{\min}$ 是确定的。你的输出 $m$ 必须等于 $m_{\min}$。注意,你输出的 $P$ 不必是一个良好的排列序列。对于给定的 $Q$,只要能够输出满足条件的 $A$,你的提交就会被判定为正确。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $3 \le N \le 500$
  • $Q$ is a permutation of $(1,2,\dots,N)$.
  • All input values are integers.
  • $3 \le N \le 500$
  • $Q$ 是 $(1,2,\dots,N)$ 的一个排列。
  • 所有输入值均为整数。
AI (CI翻译)

Input/Output

输入/输出

AI (CI翻译)

This is an interactive problem (in which your program and the judge program interact via input and output).

这是一个交互题(你的程序会通过输入输出与评测程序进行交互)。AI (CI翻译)

First, the judge gives you a positive integer $N$ in the following format:

首先,评测程序按照以下格式给出一个正整数 $N$:AI (CI翻译)

N

N
AI (CI翻译)

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+1$ 行中输出你选择的正整数 $m$ 以及 $m$ 个 $(1,2,\dots,N)$ 的排列,格式如下。其中,第 $i$ 个排列的第 $j$ 个元素为 $P_{i,j}$。请务必在末尾输出换行。AI (CI翻译)

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
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}
AI (CI翻译)

The following conditions must be satisfied:

必须满足以下条件:AI (CI翻译)

  • $m \le 1000$
  • $(P_{i,1},P_{i,2},\dots,P_{i,N})$ is a permutation of $(1,2,\dots,N)$.
  • $m \le 1000$
  • $(P_{i,1},P_{i,2},\dots,P_{i,N})$ 是 $(1,2,\dots,N)$ 的一个排列。
AI (CI翻译)

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.

如果你输出的 $P$ 不满足上述条件,评测程序将输出 -1。此时,你的提交已经被判定为错误。评测程序会在此处终止,因此建议你的程序也随之终止。AI (CI翻译)

Then, the judge gives you a permutation $Q=(Q_1,Q_2,\dots,Q_N)$ of $(1,2,\dots,N)$ in the following format:

随后,评测程序按照以下格式给出一个 $(1,2,\dots,N)$ 的排列 $Q=(Q_1,Q_2,\dots,Q_N)$:AI (CI翻译)

Q_1\ Q_2\ \dots\ Q_N

Q_1\ Q_2\ \dots\ Q_N
AI (CI翻译)

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.

然后,按照以下格式输出一个正整数序列 $A=(A_1,A_2,\dots,A_k)$。请务必在末尾输出换行。AI (CI翻译)

k\ A_1\ A_2\ \dots\ A_k

k\ A_1\ A_2\ \dots\ A_k
AI (CI翻译)

Your output is judged as correct if and only if the following conditions are all satisfied:

当且仅当以下所有条件均满足时,你的输出才会被判定为正确:AI (CI翻译)

  • $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$.
  • $m = m_{\min}$
  • $0 \le k \le 2N^2$
  • $A$ 的所有元素都在 $1$ 到 $m$ 之间(包含边界)。
  • 从序列 $R=(1,2,\dots,N)$ 开始,依次对 $i = 1,2,\dots,k$ 执行下述操作后,得到 $R = Q$。
AI (CI翻译)

Note

说明

AI (CI翻译)
  • 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.
  • 每次输出后都要换行并刷新标准输出。否则可能会被评测为 TLE。
  • 如果收到 -1,请立即终止程序。如果这样做,评测结果将为 WA;但如果不这样做,评测结果将无法确定。
  • 输出答案后请立即终止程序。否则,评测结果将无法确定。
  • 不要输出多余的换行,否则会被视为格式错误。
  • 本题的评测程序不是自适应的。评测程序会在交互开始前确定 $Q$。
AI (CI翻译)

Sample Input/Output

样例输入/输出

AI (CI翻译)

The following is a case with $N=3,Q=(2,3,1)$.

以下是 $N=3,Q=(2,3,1)$ 时的一个样例。AI (CI翻译)

Input Output Explanation
3 $N$ is given.
2 2 1 3 3 2 1 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.
输入 输出 解释
3 给出 $N$。
2 2 1 3 3 2 1 选择 $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$,并且所有内容都要在同一行中。
AI (CI翻译)
题目信息
题号
arc218_c
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。