Sequence Deletion
Time limit2sMemory limit1024 MB
Split sequence A into consecutive blocks so that the list of block mex values is lexicographically maximum, then print that list B.
- Level
Medium7 of 10
- Topics
- Greedy, Array, Implementation
- Solved
- No attempts yet
Problem
Everyone knows the functions and , which return the smallest and largest value in a set. Now consider the function , which takes a set of numbers and returns the smallest non-negative integer that is not in the set. The name comes from minimal excluded. For example, and . Magnus learned the definition of mex and immediately built a game on it. The player gets a sequence of non-negative integers and builds a sequence by repeating the following steps while still has elements: (1) Choose a positive integer that does not exceed the length of . (2) Append the mex of the first elements of to the end of . (3) Delete the first elements from . The player's task is to choose at each step so that the resulting sequence is lexicographically maximum among all possible choices. A sequence is lexicographically greater than if either there exists an with and such that , , , and , or and , , , .
Input
The first line contains the length of sequence (). The second line contains the elements (), separated by spaces.
Output
Print the length of the found sequence on the first line. On the second line, print the elements separated by spaces.