No.3256 Permutation Equation
タグ : / 解いたユーザー数 22
作問者 :
Iroha_3856
/ テスター :
champignon
Problem Statement
You are given a positive integer $N$ and a permutation $P = (P_1, P_2, \ldots, P_N)$ of $(1, 2, \ldots, N)$.
Output one permutation $Q = (Q_1, Q_2, \ldots, Q_N)$ of $(1, 2, \ldots, N)$ such that $P_i = Q_{Q_i \textcolor{red}{ + 1}}$ holds for $i = 1, 2, \ldots, N$. Here, we assume that $Q_{N+1} = Q_1$.
If no such $Q$ exists, report that fact.
Input
$N$ $P_1\ P_2\ \ldots\ P_N$
All inputs satisfy the following constraints.
- $1 \leq N \leq 2 \times 10^5$
- $P$ is a permutation of $(1, 2, \ldots, N)$
- All inputs are integers
Output
If a $Q$ satisfying the condition exists, output
Yes $Q_1\ Q_2\ \ldots\ Q_N$followed by a newline.
If no $Q$ satisfying the condition exists, output
Nofollowed by a newline.
Sample
Sample 1
Input
4 3 1 4 2
Output
Yes 2 1 3 4
$Q = (2, 1, 3, 4)$ is one of the solutions.
- $P_1 = 3$ is equal to $Q_{Q_1 + 1} = Q_{3} = 3$
- $P_2 = 1$ is equal to $Q_{Q_2 + 1} = Q_{2} = 1$
- $P_3 = 4$ is equal to $Q_{Q_3 + 1} = Q_{4} = 4$
- $P_4 = 2$ is equal to $Q_{Q_4 + 1} = Q_{5} = Q_{1} = 2$
Sample 2
Input
6 4 2 6 1 5 3
Output
No
No $Q$ satisfying the condition exists.
Sample 3
Input
7 6 5 1 3 7 2 4
Output
Yes 1 6 7 3 2 4 5
There are $4$ permutations $Q$ satisfying the condition: $(1, 6, 7, 3, 2, 4, 5), (2, 4, 6, 3, 5, 7, 1), (4, 2, 5, 3, 6, 1, 7), (5, 7, 4, 3, 1, 6, 2)$.
Outputting any of these will be judged as correct.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。