Tower
InterviewTime limit1sMemory limit1024 MB
Repeatedly delete every monochrome run of at least K cubes from a tower until none remain, then print the surviving cubes from top to bottom.
- Level
Medium5 of 10
- Topics
- Stack, Simulation
- Solved
- No attempts yet
Problem
Jonas likes playing with colored cubes. He stacked cubes one on top of another to build a tower. Jonas is unhappy with the tower. He wants to rebuild it so that no long monochrome runs of cubes remain.
To do this, Jonas removes all maximal monochrome contiguous runs of at least cubes from the tower.
For example, take the tower below and . Jonas removes the long run of green cubes. The result is the new tower below.


If the tower has several runs to remove, all of them are removed at the same time.
After the tower is rebuilt, new long runs of the same color may appear. In that case Jonas again finds all monochrome runs of length at least , removes them, and rebuilds the tower without them.
In this tower the run of red cubes has length , so Jonas rebuilds the tower again.

This rebuilding process can repeat for a long time. Find what the tower looks like after all rebuilds are finished.
Input
The first line contains two integers , the height of the tower, and .
The next lines contain the colors of the cubes in the tower, given as integers. The first line gives , the color of the top cube. The second line gives , the color of the cube below the first one. In general, line gives , the color of the cube below cube . The last line gives , the color of the bottom cube.
Output
The first line contains , the height of the tower after all rebuilds.
The next lines contain , one per line, the colors of the cubes from the top of the tower to the bottom.