Dividing the Highway into Repair Segments
Time limit1sMemory limit128 MB
Choose a start offset s in [1,m] for tiling the number line into length-m segments, minimizing how many segments contain at least one of the given damaged kilometers, and list all optimal s.
- Level
Medium7 of 10
- Topics
- Math, Sorting, Greedy, Implementation
- Solved
- No attempts yet
Problem
Bajtazar mastered the art of stacking blocks on a rectangular board, and thanks to his exceptional dexterity he was quickly promoted and joined the Ministry of Infrastructure. His job is to optimize the work of construction crews, and right now he is in charge of repairing highway A1.
The kilometers of the highway are numbered . The highway is split into segments, each kilometers long. If the first segment starts at kilometer , then the -th segment starts at kilometer ; that is, the -th segment covers kilometers through .
For every kilometer of the highway we know whether it needs repair. A construction crew must be sent to every -kilometer segment that contains at least one kilometer needing repair. The goal is to split the highway into segments so that the number of crews sent is as small as possible.
The first segment must start at one of the first kilometers, so . In addition, none of the first kilometers of the highway needs repair.
Write a program that:
- reads the description of the highway damage from standard input,
- finds a split into segments that minimizes the required number of crews,
- writes the result to standard output.
Input
The first line contains two integers and (): the length of a single segment and the number of kilometers needing repair.
The second line contains integers in increasing order, separated by single spaces (). Each denotes one kilometer of the highway that needs repair.
Output
In the first line, output the minimum number of construction crews sent.
In the second line, output every position at which the first segment may start, that is, every start kilometer that minimizes the number of crews, in increasing order and separated by single spaces.