Box (Codeforces 1227B)
Tags: Others, constructive algorithms

Permutation $$$p$$$ is a sequence of integers $$$p=[p_1, p_2, \dots, p_n]$$$, consisting of $$$n$$$ distinct (unique) positive integers between $$$1$$$ and $$$n$$$, inclusive. For example, the following sequences are permutations: $$$[3, 4, 1, 2]$$$, $$$[1]$$$, $$$[1, 2]$$$. The following sequences are not permutations: $$$[0]$$$, $$$[1, 2, 1]$$$, $$$[2, 3]$$$, $$$[0, 1, 2]$$$.The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $$$p$$$ of length $$$n$$$. You don't know this permutation, you only know the array $$$q$$$ of prefix maximums of this permutation. Formally:  $$$q_1=p_1$$$,  $$$q_2=\max(p_1, p_2)$$$,  $$$q_3=\max(p_1, p_2,p_3)$$$,  ...  $$$q_n=\max(p_1, p_2,\dots,p_n)$$$. You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $$$q$$$ for this permutation is equal to the given array).

Input: The first line contains integer number $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow.The first line of a test case contains one integer $$$n$$$ $$$(1 \le n \le 10^{5})$$$ — the number of elements in the secret code permutation $$$p$$$.The second line of a test case contains $$$n$$$ integers $$$q_1, q_2, \dots, q_n$$$ $$$(1 \le q_i \le n)$$$ — elements of the array $$$q$$$ for secret permutation. It is guaranteed that $$$q_i \le q_{i+1}$$$ for all $$$i$$$ ($$$1 \le i < n$$$).The sum of all values $$$n$$$ over all the test cases in the input doesn't exceed $$$10^5$$$.

Output: For each test case, print:  If it's impossible to find such a permutation $$$p$$$, print "-1" (without quotes).  Otherwise, print $$$n$$$ distinct integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$). If there are multiple possible answers, you can print any of them. 

Note: In the first test case of the example answer $$$[1,3,4,5,2]$$$ is the only possible answer:  $$$q_{1} = p_{1} = 1$$$;  $$$q_{2} = \max(p_{1}, p_{2}) = 3$$$;  $$$q_{3} = \max(p_{1}, p_{2}, p_{3}) = 4$$$;  $$$q_{4} = \max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$$$;  $$$q_{5} = \max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$$$. It can be proved that there are no answers for the second test case of the example.

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

Sample Outputs:
1 3 4 5 2 
-1
2 1 
1

Solution (Python):
""""""

def construct_permutation(n, q):
    if q[0] < 1 or q[0] > n:
        return -1
    permutation = [q[0]]
    max_element = q[0]
    min_not_use = 1
    used = set()
    used.add(max_element)
    for i in range(1, n):
        if q[i] < max_element:
            return -1
        elif q[i] > max_element:
            permutation.append(q[i])
            max_element = q[i]
            used.add(max_element)
        else:
            temp = min_not_use
            while temp in used:
                temp += 1
                if temp >= max_element:
                    return -1
            permutation.append(temp)
            used.add(temp)
            min_not_use = temp + 1
    return permutation
t = int(input())
for _ in range(t):
    n = int(input())
    q = list(map(int, input().split()))
    result = construct_permutation(n, q)
    if result == -1:
        print('-1')
    else:
        print(' '.join(map(str, result)))

Solution (Python):
test_case_quantity = int(input())
while test_case_quantity > 0:
    test_case_quantity -= 1
    can_be_founded = True
    permutation_length = int(input())
    used = [False] * (permutation_length + 2)
    maximums_prefix = list(map(int, input().split()))
    permutation = [str(maximums_prefix[0])]
    used[maximums_prefix[0]] = True
    minimum_unrevealed_integer = 1
    if minimum_unrevealed_integer == int(permutation[0]):
        minimum_unrevealed_integer += 1
    maximum_revealed_integer = maximums_prefix[0]
    for i in range(1, permutation_length):
        to_be_appended = 0
        if maximum_revealed_integer < maximums_prefix[i]:
            maximum_revealed_integer = maximums_prefix[i]
            to_be_appended = str(maximums_prefix[i])
            used[maximums_prefix[i]] = True
        else:
            while used[minimum_unrevealed_integer] == True:
                minimum_unrevealed_integer += 1
            used[minimum_unrevealed_integer] = True
            to_be_appended = str(minimum_unrevealed_integer)
        if int(to_be_appended) > maximums_prefix[i]:
            print(-1)
            can_be_founded = False
            break
        permutation.append(to_be_appended)
    if can_be_founded:
        print(' '.join(permutation))

Solution (Python):
import sys
T = int(input())
for _ in range(T):
    N = int(input())
    p = list(map(int, input().split()))
    res = []
    used = [False] * (N + 1)
    u_idx = 1
    p.insert(0, 0)
    impossible = False
    for i in range(1, len(p)):
        if p[i] > p[i - 1]:
            res.append(p[i])
            used[p[i]] = True
        else:
            while used[u_idx]:
                u_idx += 1
            if u_idx > p[i]:
                impossible = True
                break
            res.append(u_idx)
            used[u_idx] = True
    if impossible:
        print(-1)
    else:
        print(*res)