Complete Road
InterviewTime limit1sMemory limit1024 MB
Given hole positions and segment length m, find the fewest damaged segments over all start offsets k from 1 to m, and list every k that reaches it.
- Level
Medium5 of 10
- Topics
- Math, Prefix sum, Array
- Solved
- No attempts yet
Problem
In our beautiful homeland, roads are the pride of its citizens, especially the superhighway with the wonderfully named . Unfortunately, many tourists with their Trabants loaded with cheap pâté and cans of beer damage the road every year, so it must be repaired.
The State Institute for Road Integrity inspected the highway and marked every hole that needs repair. Repairs proceed as follows. First, starting at some point before the first damage, the road is divided into segments of equal length. Then one excavator with its crew is sent to each segment that contains damage.
Because the country has too few excavators, traffic experts must decide how to divide the road into segments of a given length so that the number of damaged segments is as small as possible.
The road has holes, each given as an integer that is its distance from the start of the road. The length of each segment is a given natural number . There is no damage in the first meters of the road. The first segment must start at one of the first meters. If the first segment starts at meter , then the -th segment starts at meter . One excavator can cover all holes from the start of a segment (inclusive) up to the start of the next segment (exclusive).
Write a program that finds the minimum number of excavators needed to repair the highway and all possible positions where the first segment can start.
Input
The first line contains the natural numbers and () from the problem statement.
The second line contains natural numbers (), the positions of all holes on the road.
Output
The first line contains the minimum number of excavators needed to repair the highway.
The second line contains all positions where the first segment can start, in increasing order, separated by single spaces.