Codeforces Round 1085 (Div. 1 + Div. 2)
E1
N-MEX (Constructive Version)
Codeforces
2200
原题
← DE2 →
翻译中…

This is the constructive version of the problem. The difference between the versions is that in this version, you need to either construct an answer or report that it is impossible, and $0 \leq b_i \leq 10^9$. You can hack only if you solved all versions of this problem.

翻译中…

The Master Builder doesn't like repetitive tasks — repairing the base, upgrading the town hall fifteen times, and doing yet more programming problems about MEX. So, this is not going to be your ordinary MEX problem.

翻译中…

For any positive integer $k$, define the $k$-mex of a collection of integers $S$ to be the $k$-th smallest nonnegative integer not present in $S$. For instance, the $1$-mex and $2$-mex of $[1, 2, 1]$ are $0$ and $3$, respectively.

翻译中…

Let $n$ be a positive integer, and consider an array of nonnegative integers $[a_1, \ldots, a_n]$. Determine whether or not there exists an array of nonnegative integers $[b_1, \ldots, b_n]$ such that:

翻译中…
  • For all $1 \leq i \leq n$, the $(n-i+1)$-mex of $[b_1, \ldots, b_i]$ is $a_i$.
翻译中…

Additionally, if such an array $b$ exists, find any such $[b_1, \ldots, b_n]$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$.

翻译中…

The second line of each test case contains $n$ integers $a_1, \ldots, a_n$ ($0 \leq a_i \leq 10^9$).

翻译中…

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

翻译中…

Output

翻译中…

For each test case, print a line containing either "YES" or "NO", representing whether or not the array $b$ exists. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

翻译中…

Then, if you responded with "YES", print a second line containing $n$ integers $b_1, \ldots, b_n$ ($0 \leq b_i \leq 10^9$) such that for all $1 \leq i \leq n$, the $(n-i+1)$-mex of $[b_1, \ldots, b_i]$ is $a_i$.

翻译中…

Note

翻译中…

In the first test case, the array $a = [3, 3, 1]$. We can check that the array $b = [2, 0, 2]$ satisfies the conditions because:

翻译中…
  • the $3$-mex of $[b_1] = [2]$ is $a_1 = 3$,
  • the $2$-mex of $[b_1, b_2] = [2, 0]$ is $a_2 = 3$,
  • the $1$-mex of $[b_1, b_2, b_3] = [2, 0, 2]$ is $a_3 = 1$.
翻译中…

In the second test case, the array $a = [2, 1, 3]$. It can be shown that no suitable array $b$ exists.

翻译中…

In the third test case, the array $a = [0]$. We can check that the array $b = [67]$ satisfies the conditions because the $1$-mex of $[b_1] = [67]$ is $a_1 = 0$.

翻译中…

样例

Input #1
6
3
3 3 1
3
2 1 3
1
0
1
2
4
7 5 2 2
6
6 6 6 4 3 3
Output #1
YES
2 0 2
NO
YES
67
NO
NO
YES
5 2 1 5 2 0
InputCopy OutputCopy
题目信息
题号
2207E1
时间限制
2 s
内存限制
256 MB
标签
构造
贪心

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