Restoring the Permutation (Codeforces 1506E)
Tags: Others, implementation

A permutation is a sequence of $$$n$$$ integers from $$$1$$$ to $$$n$$$, in which all numbers occur exactly once. For example, $$$[1]$$$, $$$[3, 5, 2, 1, 4]$$$, $$$[1, 3, 2]$$$ are permutations, and $$$[2, 3, 2]$$$, $$$[4, 3, 1]$$$, $$$[0]$$$ are not.Polycarp was presented with a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$. However, when Polycarp came home, he noticed that in his pocket, the permutation $$$p$$$ had turned into an array $$$q$$$ according to the following rule:   $$$q_i = \max(p_1, p_2, \ldots, p_i)$$$. Now Polycarp wondered what lexicographically minimal and lexicographically maximal permutations could be presented to him.An array $$$a$$$ of length $$$n$$$ is lexicographically smaller than an array $$$b$$$ of length $$$n$$$ if there is an index $$$i$$$ ($$$1 \le i \le n$$$) such that the first $$$i-1$$$ elements of arrays $$$a$$$ and $$$b$$$ are the same, and the $$$i$$$-th element of the array $$$a$$$ is less than the $$$i$$$-th element of the array $$$b$$$. For example, the array $$$a=[1, 3, 2, 3]$$$ is lexicographically smaller than the array $$$b=[1, 3, 4, 2]$$$.For example, if $$$n=7$$$ and $$$p=[3, 2, 4, 1, 7, 5, 6]$$$, then $$$q=[3, 3, 4, 4, 7, 7, 7]$$$ and the following permutations could have been as $$$p$$$ initially:   $$$[3, 1, 4, 2, 7, 5, 6]$$$ (lexicographically minimal permutation);  $$$[3, 1, 4, 2, 7, 6, 5]$$$;  $$$[3, 2, 4, 1, 7, 5, 6]$$$;  $$$[3, 2, 4, 1, 7, 6, 5]$$$ (lexicographically maximum permutation). For a given array $$$q$$$, find the lexicographically minimal and lexicographically maximal permutations that could have been originally presented to Polycarp.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow.The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).The second line of each test case contains $$$n$$$ integers $$$q_1, q_2, \ldots, q_n$$$ ($$$1 \le q_i \le n$$$).It is guaranteed that the array $$$q$$$ was obtained by applying the rule from the statement to some permutation $$$p$$$.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case, output two lines:   on the first line output $$$n$$$ integers — lexicographically minimal permutation that could have been originally presented to Polycarp;  on the second line print $$$n$$$ integers — lexicographically maximal permutation that could have been originally presented to Polycarp; 


Sample Inputs:
4
7
3 3 4 4 7 7 7
4
1 2 3 4
7
3 4 5 5 5 7 7
1
1

Sample Outputs:
3 1 4 2 7 5 6 
3 2 4 1 7 6 5 
1 2 3 4 
1 2 3 4 
3 4 5 1 2 7 6 
3 4 5 2 1 7 6 
1 
1

Solution (Python):
from sys import stdin, stdout
(input, print) = (stdin.readline, stdout.write)

def main():
    t = int(input())
    while t > 0:
        t -= 1
        n = int(input())
        q = [*map(int, input().split())]
        x = [0] * n
        u = []
        p = 0
        for i in range(n):
            v = q[i]
            if v != p:
                x[i] = v
                u += [v]
                p = v
        y = x[:]
        e = []
        p = 0
        u = u[::-1]
        l = 1
        for i in range(n):
            if x[i] < 1:
                while l and l == u[-1]:
                    l += 1
                    u.pop()
                x[i] = l
                l += 1
                y[i] = e.pop()
            else:
                e += [*range(p + 1, x[i])]
                p = x[i]
        print(' '.join(map(str, x)))
        print('\n')
        print(' '.join(map(str, y)))
        print('\n')
main()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline

class Problem:

    def __init__(self):
        pass

    def solve(self):
        ans = 0
        ans = 0
        n = int(input())
        A = list(map(int, input().split()))
        dqs = deque()
        dql = deque()
        a = 1
        while a < A[0]:
            dqs.append(a)
            dql.append(a)
            a += 1
        ans = [[A[0]], [A[0]]]
        for i in range(1, n):
            if A[i] == A[i - 1]:
                ans[0].append(dqs.popleft())
                ans[1].append(dql.pop())
            else:
                a = A[i - 1] + 1
                while a < A[i]:
                    dqs.append(a)
                    dql.append(a)
                    a += 1
                ans[0].append(A[i])
                ans[1].append(A[i])
        ans[0] = list(map(str, ans[0]))
        ans[1] = list(map(str, ans[1]))
        print(' '.join(ans[0]))
        print(' '.join(ans[1]))

def main():
    p = Problem()
    t = int(input())
    while t:
        p.solve()
        t -= 1
if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin, stdout

def solve():
    n = int(input())
    q = [int(x) for x in stdin.readline().split()]
    amin = []
    used = [False for _ in range(n + 1)]
    l = 1
    for i in range(n):
        if i == 0 or q[i] > q[i - 1]:
            amin.append(q[i])
            used[q[i]] = True
        else:
            while used[l]:
                l += 1
            amin.append(l)
            l += 1
    amax = []
    l = 1
    z = []
    for i in range(n):
        if i == 0 or q[i] > q[i - 1]:
            while l <= q[i]:
                z.append(l)
                l += 1
        amax.append(z.pop())
    stdout.write(' '.join(map(str, amin)) + '\n')
    stdout.write(' '.join(map(str, amax)) + '\n')
t = int(input())
for _ in range(t):
    solve()