cswork / OA打开练习工作台

SnowflakeOA #19

Array Reduction Algorithm

估计中等可在线判题

考点(AI 标注)贪心调度哈希计数

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.

输入格式

The first line contains n, and the second line contains n values. 1≤n≤100000, 0≤arr[i]≤n.

输出格式

First output the length of the result, then output the corresponding integer array.

样例 1

输入:

4
0 1 0 2

输出:

1
3

样例 2

输入:

3
1 1 1

输出:

3
0 0 0

样例 3

输入:

6
0 1 2 0 1 2

输出:

2
3 3

样例说明

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.