Max or Min
Time limit1sMemory limit256 MB
Given numbers on a circle and min/max operations over a value and its two neighbors, find for each x the least minutes to make all values equal x, or -1.
- Level
Hard8 of 10
- Topics
- Implementation, Greedy, Math, Simulation
- Solved
- No attempts yet
Problem
Kevin has integers arranged in a circle. That is, and () are neighbors, and and are neighbors as well. Therefore, each number has exactly two neighbors.
In one minute, Kevin can set to the minimum among the three numbers and its two neighbors. Alternatively, he can set to the maximum among the same three numbers. For example, if and its two neighbors are 3 and 2, and Kevin performs the minimum operation, becomes 2. If he performs the maximum operation, however, stays 5.
For each from 1 to , find the minimum number of minutes needed to make all numbers equal , or determine that it is impossible.
Input
The first line contains two integers and (, ): the number of integers in the circle and the number of values you need answers for.
The second line contains integers ().
Output
Print integers. The -th integer is the minimum number of minutes needed to make all numbers equal , or if it is impossible.
Hint
To make all numbers equal 2, Kevin needs at least 5 minutes. One possible sequence of operations:
- Apply the minimum operation to . It becomes 2.
- Apply the maximum operation to . It becomes 2.
- Apply the maximum operation to . It becomes 5.
- Apply the minimum operation to . It becomes 2.
- Apply the minimum operation to . It becomes 2.