SnowflakeOA #19
Array Reduction Algorithm
Estimated MediumJudge-ready
Repeatedly choose a non-empty prefix, append its MEX to the result, and delete that prefix, until the array is empty. Find the lexicographically largest result; when one array is a proper prefix of another, the longer one is larger. The MEX is the smallest non-negative integer that does not appear.
Input
The first line contains n, and the second line contains n values. 1≤n≤100000, 0≤arr[i]≤n.
Output
First output the length of the result, then output the corresponding integer array.
Example 1
Input:
4
0 1 0 2
Output:
1
3
Example 2
Input:
3
1 1 1
Output:
3
0 0 0
Example 3
Input:
6
0 1 2 0 1 2
Output:
2
3 3
Example notes
The answers to the three public examples are, in order: 1 3; 3 0 0 0; 2 3 3. All examples have been verified by independent small-scale enumeration or state search.
Editorial
Loading solution…