Permutation Addicts
Contest: Codeforces Global Round 22
Difficulty rating: 1900
Tags: constructive algorithms, data structures, dfs and similar, dsu, graphs, trees

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 < i$$$) that $$$a_j > k$$$. If no such element $$$a_j$$$ exists, set $$$b_{x} = n+1$$$.
- If $$$x > k$$$, set $$$b_{x}$$$ to the last element $$$a_j$$$ ($$$1 \leq j < 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 format:
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 format:
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.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

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 < i$$$) that $$$a_j > k$$$. Therefore, $$$b_1 = n + 1 = 5$$$.
- When $$$i = 2$$$, $$$x = a_i = 3 > 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 > k$$$ is $$$a_2$$$. Therefore, $$$b_2 = a_2 = 3$$$.
- When $$$i = 4$$$, $$$x = a_i = 4 > k$$$, the last element $$$a_j$$$ that $$$a_j \leq k$$$ is $$$a_3$$$. Therefore, $$$b_4 = a_3 = 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 < i$$$) that $$$a_j > k$$$. Therefore, $$$b_1 = b_2 = b_3 = n + 1 = 7$$$.
- When $$$i = 4, 5, 6$$$, $$$a_i > k$$$, the last element $$$a_j$$$ that $$$a_j \leq k$$$ is $$$a_3$$$. Therefore, $$$b_4 = b_5 = b_6 = a_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 > k$$$, there is no $$$a_j$$$ ($$$1 \leq j < 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 > k$$$ is $$$a_3$$$. Therefore, $$$b_1 = b_2 = b_3 = a_3 = 4$$$.

Example input:
3
4
5 3 1 2
6
7 7 7 3 3 3
6
4 4 4 0 0 0

Example output:
2
1 3 2 4
3
1 2 3 4 5 6
3
6 5 4 3 2 1

Editorial:
For readability and convenience of the readers who are interested in the checker of this problem, we consider this problem without assuming the existence of the threshold $$$k$$$ and permutation $$$a_1, a_2, \dots, a_n$$$.
Let's first determine the value of $$$k$$$. A valid sequence $$$b$$$ must satisfy that
• For every $$$i \leq k$$$, we have $$$b_i > i$$$;
• For every $$$i > k$$$, we have $$$b_i < i$$$.
• For every $$$i \leq k$$$, we have $$$b_i > k$$$;
• For every $$$i > k$$$, we have $$$b_i \leq k$$$.
Now $$$k$$$ is fixed. We are going to find a sequence $$$a$$$ that produces sequence $$$b$$$ with respect to $$$k$$$. We construct a directed graph $$$G$$$ with $$$n+2$$$ vertices numbered from $$$0$$$ to $$$n+1$$$ as follows.
• For every $$$1 \leq i \leq n$$$, add an edge from vertex $$$b_i$$$ to vertex $$$i$$$.
It is clear that there are $$$n$$$ edges in graph $$$G$$$. We consider two parts of the graph $$$G$$$, set $$$A$$$ of vertices from $$$0$$$ to $$$k$$$ and set $$$B$$$ of vertices from $$$k+1$$$ to $$$n+1$$$. Then every directed edge is either from $$$A$$$ to $$$B$$$ or from $$$B$$$ to $$$A$$$.
Claim B.1. There is exactly one vertex between vertex $$$0$$$ and vertex $$$n+1$$$ that is isolated (that is, no edges are incident to it).
Proof: First of all, we show that it cannot be the case that both vertex $$$0$$$ and vertex $$$n+1$$$ are isolated. This is because $$$b_{a_1}$$$ is either $$$0$$$ or $$$n+1$$$ according to its definition.
Now suppose both vertex $$$0$$$ and vertex $$$n+1$$$ are not isolated. Then there are two different indexes $$$x$$$ and $$$y$$$ such that $$$b_x = 0$$$ and $$$b_y = n+1$$$ with $$$1 \leq y \leq k < x \leq n$$$. Find two different indexes $$$i$$$ and $$$j$$$ such that $$$a_i = x$$$ and $$$a_j = y$$$.
• If $$$i < j$$$, then we have $$$a_i = x > k \geq a_j = y$$$. By the definition of $$$b_{y}$$$, $$$a_i = x$$$ is a candidate. So $$$b_y \neq n+1$$$.
• If $$$i > j$$$, then we have $$$a_j = y \leq k < a_i = x$$$. By the definition of $$$b_{x}$$$, $$$a_j = y$$$ is a candidate. So $$$b_x \neq 0$$$.
By Claim B.1, we can just ignore the isolated vertex. After that, there are $$$n+1$$$ vertices and $$$n$$$ edges. It seems like that the graph $$$G$$$ is a tree! Next, we will show that this is true.
Claim B.2. The graph $$$G$$$ must not contain loops. That is, $$$G$$$ is a DAG (directed acyclic graph).
Proof: There is no edge leading to vertex $$$0$$$ or $$$n + 1$$$. So loops will only occur among vertices from $$$1$$$ to $$$n$$$.
Every edge $$$(u, v)$$$ in graph $$$G$$$ for $$$1 \leq u, v \leq n$$$ means that, in sequence $$$a$$$, the value of $$$u$$$ is in front of the value of $$$v$$$. Since sequence $$$a$$$ is a permutation, all values appear exactly once. A loop implies that there are two different vertices $$$u$$$ and $$$v$$$ such that $$$u$$$ is in front of $$$v$$$ and $$$v$$$ is in front of $$$u$$$. This is of course imposssible. $$$\Box$$$
According to the construction of graph $$$G$$$, there is at most one edge leading to each vertex. So DAG $$$G$$$ is a rooted tree with vertex $$$0$$$ or $$$n+1$$$, with every edge $$$(u, v)$$$ meaning that $$$u$$$ is the parent of $$$v$$$.
Now we have a tree $$$G$$$, and want to find a suitable sequence $$$a$$$.
Claim B.3. For every vertex $$$u$$$ in tree $$$G$$$, there is at most one child vertex $$$v$$$ of $$$u$$$ that is not a leaf.
Proof: If there are two child vertices $$$v_1$$$ and $$$v_2$$$ of $$$u$$$ that are not leaves, let $$$w_1$$$ and $$$w_2$$$ be child vertices of $$$v_1$$$ and $$$v_2$$$, respectively. Let $$$a^{-1}(x)$$$ the index $$$i$$$ of $$$x$$$ such that $$$a_i = x$$$. In this notation, we have $$$a^{-1}(u) < a^{-1}(v)$$$ for every edge $$$(u, v)$$$ in tree $$$G$$$. Without loss of generality, we assume that $$$a^{-1}(v_1) < a^{-1}(v_2)$$$.
Then, we have $$$a^{-1}(v_2) < a^{-1}(w_1)$$$. If this is not true, i.e., $$$a^{-1}(v_2) > a^{-1}(w_1)$$$, then $$$w_1$$$ is a candidate for $$$b_{v_2}$$$, which leads that $$$u$$$ is no longer the parent vertex of $$$v_2$$$.
Now we have $$$a^{-1}(v_2) < a^{-1}(w_1)$$$. This means that $$$v_2$$$ is a candidate for $$$b_{w_1}$$$, which leads that $$$v_1$$$ is no longer the parent vertex of $$$w_1$$$. A contradiction! $$$\Box$$$
Now we are ready to give an algorithm to find a suitable sequence $$$a$$$ with tree $$$G$$$, which is rather simple:
• Find the BFS order of tree $$$G$$$, with non-leaf vertices visited last.
Before processing the BFS, remember to check the graph $$$G$$$ as follows:
1. There is exactly one isolated vertex between vertex $$$0$$$ and $$$n+1$$$.
2. The graph $$$G$$$ is a DAG, i.e., no loops exist in graph $$$G$$$.
3. With the above, graph $$$G$$$ must be a rooted tree. Choose the non-isolated vertex from vertex $$$0$$$ and $$$n+1$$$ as the root.
4. For every vertex $$$u$$$ in tree $$$G$$$, there is at most one non-leaf child vertex of $$$u$$$.