Meximum Array (Codeforces 1628A)
Tags: Greedy, two pointers

Mihai has just learned about the MEX concept and since he liked it so much, he decided to use it right away.Given an array $$$a$$$ of $$$n$$$ non-negative integers, Mihai wants to create a new array $$$b$$$ that is formed in the following way:While $$$a$$$ is not empty:   Choose an integer $$$k$$$ ($$$1 \leq k \leq |a|$$$).  Append the MEX of the first $$$k$$$ numbers of the array $$$a$$$ to the end of array $$$b$$$ and erase them from the array $$$a$$$, shifting the positions of the remaining numbers in $$$a$$$. But, since Mihai loves big arrays as much as the MEX concept, he wants the new array $$$b$$$ to be the lexicographically maximum. So, Mihai asks you to tell him what the maximum array $$$b$$$ that can be created by constructing the array optimally is.An array $$$x$$$ is lexicographically greater than an array $$$y$$$ if in the first position where $$$x$$$ and $$$y$$$ differ $$$x_i > y_i$$$ or if $$$|x| > |y|$$$ and $$$y$$$ is a prefix of $$$x$$$ (where $$$|x|$$$ denotes the size of the array $$$x$$$).The MEX of a set of non-negative integers is the minimal non-negative integer such that it is not in the set. For example, MEX({$$${1, 2, 3}$$$}) $$$= 0$$$ and MEX({$$${0, 1, 2, 4, 5}$$$}) $$$= 3$$$.

Input: The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of elements in the array $$$a$$$.The second line of each test case contains $$$n$$$ non-negative integers $$$a_1, \ldots, a_n$$$ ($$$0 \leq a_i \leq n$$$), where $$$a_i$$$ is the $$$i$$$-th integer from the array $$$a$$$.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 $$$m$$$ — the length of the maximum array $$$b$$$ Mihai can create, followed by $$$m$$$ integers denoting the elements of the array $$$b$$$.

Note: In the first test case, the lexicographically maximum array $$$b$$$ is obtained by selecting $$$k=5$$$, resulting in the $$$MEX$$$ of the whole array $$$a$$$. It is lexicographically maximum because an array starting with a smaller number than $$$4$$$ is lexicographically smaller, and choosing a $$$k<5$$$ would result in an array starting with a number smaller than $$$4$$$.In the second test case, there are two ways to obtain the maximum array: first selecting $$$k=6$$$, then $$$k=2$$$, or first selecting $$$k=7$$$ and then $$$k=1$$$.

Sample Inputs:
6
5
1 0 2 0 3
8
2 2 3 4 0 1 2 0
1
1
5
0 1 2 3 4
4
0 1 1 0
10
0 0 2 1 1 1 0 0 1 1

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

Solution (Python):
def solve(a):
    n = len(a)
    post_mex = [0] * n
    nums = set()
    cur_mex = 0
    for i in range(n - 1, -1, -1):
        nums.add(a[i])
        while cur_mex in nums:
            cur_mex += 1
        post_mex[i] = cur_mex
    i = j = 0
    b = []
    while i < n:
        nums = set()
        cur_mex = 0
        while i < n:
            nums.add(a[i])
            while cur_mex in nums:
                cur_mex += 1
            if cur_mex == post_mex[j]:
                break
            i += 1
        b.append(cur_mex)
        i += 1
        j = i
    return b
for _ in range(int(input())):
    input()
    b = solve(list(map(int, input().split())))
    print(len(b))
    print(' '.join(map(str, b)))

Solution (Python):
from sys import stdin, stdout

def pr(out):
    return stdout.write(f'{out}\n')

def ver_in():
    return stdin.readline()

def hor_in():
    return list(map(int, stdin.readline().split()))

def solve():
    n = int(ver_in())
    nums = hor_in()
    counter = [0] * (n + 1)
    for num in nums:
        counter[num] += 1
    numbers_left = n
    prefix = [0] * (n + 1)
    at = 0
    ans = []
    while numbers_left > 0:
        mex = 0
        while counter[mex] != 0:
            mex += 1
        newMex = -1
        modified = []
        while newMex < mex:
            if newMex == -1:
                newMex = 0
            nextVal = nums[at]
            modified.append(nextVal)
            prefix[nextVal] += 1
            counter[nextVal] -= 1
            numbers_left -= 1
            while prefix[newMex] != 0:
                newMex += 1
            at += 1
        for num in modified:
            prefix[num] = 0
        ans.append(newMex)
    pr(len(ans))
    pr(' '.join((str(ch) for ch in ans)))
if __name__ == '__main__':
    t = int(ver_in())
    for _ in range(t):
        solve()

Solution (Python):
import sys
import threading
from random import randint
import os
from io import BytesIO, IOBase
from collections import defaultdict, Counter, deque
from math import inf, sqrt, floor
from functools import lru_cache
from heapq import heappop, heappush, heapify
import math
from bisect import bisect_left, bisect_right
nextInts = lambda : list(map(int, input().split()))
nextInt = lambda : int(input())
Mod = 10 ** 9 + 7

def build_prefix(arr):
    prefix = [-1] * len(arr)
    all = set()
    mer = 0
    for i in range(len(arr))[::-1]:
        all.add(arr[i])
        while mer in all:
            mer += 1
        prefix[i] = mer
    return prefix

def solution():
    n = nextInt()
    arr = nextInts()
    prefix = build_prefix(arr)
    b = []
    all = set()
    mer = 0
    global_mer = prefix[0]
    for i in range(len(arr)):
        all.add(arr[i])
        while mer in all:
            mer += 1
        if mer == global_mer or i == len(arr) - 1:
            b.append(mer)
            if i + 1 < len(arr):
                global_mer = prefix[i + 1]
            mer = 0
            all = set()
    print(len(b))
    print(*b)

def main():
    t = 1
    t = int(input())
    for _ in range(t):
        solution()
main()