Codeforces Global Round 22
D
Permutation Addicts
Codeforces
1900
原题
← CE →
翻译中…

Given a permutation $a_1, a_2, \dots, a_n$ of integers from $1$ to $n$, and a threshold $k$ with $0 \leq k \leq n$, you compute a sequence $b_1, b_2, \dots, b_n$ as follows.

翻译中…

For every $1 \leq i \leq n$ in increasing order, let $x = a_i$.

翻译中…
  • If $x \leq k$, set $b_{x}$ to the last element $a_j$ ($1 \leq j \lt i$) that $a_j \gt k$. If no such element $a_j$ exists, set $b_{x} = n+1$.
  • If $x \gt k$, set $b_{x}$ to the last element $a_j$ ($1 \leq j \lt i$) that $a_j \leq k$. If no such element $a_j$ exists, set $b_{x} = 0$.
翻译中…

Unfortunately, after the sequence $b_1, b_2, \dots, b_n$ has been completely computed, the permutation $a_1, a_2, \dots, a_n$ and the threshold $k$ are discarded.

翻译中…

Now you only have the sequence $b_1, b_2, \dots, b_n$. Your task is to find any possible permutation $a_1, a_2, \dots, a_n$ and threshold $k$ that produce the sequence $b_1, b_2, \dots, b_n$. It is guaranteed that there exists at least one pair of permutation $a_1, a_2, \dots, a_n$ and threshold $k$ that produce the sequence $b_1, b_2, \dots, b_n$.

翻译中…

A permutation of integers from $1$ to $n$ is a sequence of length $n$ which contains all integers from $1$ to $n$ exactly once.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The following lines contain the description of each test case.

翻译中…

The first line of each test case contains an integer $n$ ($1 \leq n \leq 10^5$), indicating the length of the permutation $a$.

翻译中…

The second line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \leq b_i \leq n+1$), indicating the elements of the sequence $b$.

翻译中…

It is guaranteed that there exists at least one pair of permutation $a_1, a_2, \dots, a_n$ and threshold $k$ that produce the sequence $b_1, b_2, \dots, b_n$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

翻译中…

Output

翻译中…

For each test case, output the threshold $k$ ($0 \leq k \leq n$) in the first line, and then output the permutation $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$) in the second line such that the permutation $a_1, a_2, \dots, a_n$ and threshold $k$ produce the sequence $b_1, b_2, \dots, b_n$. If there are multiple solutions, you can output any of them.

翻译中…

Note

翻译中…

For the first test case, permutation $a = [1,3,2,4]$ and threshold $k = 2$ will produce sequence $b$ as follows.

翻译中…
  • When $i = 1$, $x = a_i = 1 \leq k$, there is no $a_j$ ($1 \leq j \lt i$) that $a_j \gt k$. Therefore, $b_1 = n + 1 = 5$.
  • When $i = 2$, $x = a_i = 3 \gt k$, the last element $a_j$ that $a_j \leq k$ is $a_1$. Therefore, $b_3 = a_1 = 1$.
  • When $i = 3$, $x = a_i = 2 \leq k$, the last element $a_j$ that $a_j \gt k$ is $a_2$. Therefore, $b_2 = a_2 = 3$.
  • When $i = 4$, $x = a_i = 4 \gt k$, the last element $a_j$ that $a_j \leq k$ is $a_3$. Therefore, $b_4 = a_3 = 2$.
翻译中…

Finally, we obtain sequence $b = [5,3,1,2]$.

翻译中…

For the second test case, permutation $a = [1,2,3,4,5,6]$ and threshold $k = 3$ will produce sequence $b$ as follows.

翻译中…
  • When $i = 1, 2, 3$, $a_i \leq k$, there is no $a_j$ ($1 \leq j \lt i$) that $a_j \gt k$. Therefore, $b_1 = b_2 = b_3 = n + 1 = 7$.
  • When $i = 4, 5, 6$, $a_i \gt k$, the last element $a_j$ that $a_j \leq k$ is $a_3$. Therefore, $b_4 = b_5 = b_6 = a_3 = 3$.
翻译中…

Finally, we obtain sequence $b = [7,7,7,3,3,3]$.

翻译中…

For the third test case, permutation $a = [6,5,4,3,2,1]$ and threshold $k = 3$ will produce sequence $b$ as follows.

翻译中…
  • When $i = 1, 2, 3$, $a_i \gt k$, there is no $a_j$ ($1 \leq j \lt i$) that $a_j \leq k$. Therefore, $b_4 = b_5 = b_6 = 0$.
  • When $i = 4, 5, 6$, $a_i \leq k$, the last element $a_j$ that $a_j \gt k$ is $a_3$. Therefore, $b_1 = b_2 = b_3 = a_3 = 4$.
翻译中…

Finally, we obtain sequence $b = [4,4,4,0,0,0]$.

翻译中…

样例

Input #1
3
4
5 3 1 2
6
7 7 7 3 3 3
6
4 4 4 0 0 0
Output #1
2
1 3 2 4
3
1 2 3 4 5 6
3
6 5 4 3 2 1
InputCopy OutputCopy
题目信息
题号
1738D
时间限制
2 s
内存限制
512 MB
标签
构造
数据结构
DFS 及类似
并查集
图论
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。