Key Insertion
Time limit1sMemory limit512 MB
Simulate the recursive Insert operation on an infinite array for N keys and print the final occupancy up to the largest filled cell.
- Level
Hard8 of 10
- Topics
- Union-find, Implementation, Array, Simulation
- Solved
- No attempts yet
Problem
As an employee of the Macrohard Company, you have been asked to implement a new data structure for storing integer keys.
The keys are kept in a special ordered collection that behaves like an array with infinitely many cells, numbered starting from . Initially every cell is empty. The collection supports a single operation, , where is a cell index and is a positive integer.
is defined recursively:
- If cell is empty, set .
- If cell is already occupied, first perform , and then set .
You are given indices . Starting from the empty array, perform the operations , , , in this order, and output the final contents of the array.
Input
The first line contains two integers and : the number of Insert operations and the largest cell index that may be used in an operation (, ).
The second line contains integers describing the operations to perform ().
Output
Output the contents of the array after all of the operations have been performed. On the first line, print , the largest index of a non-empty cell. On the second line, print the integers , using for empty cells.