#P1440. Minimum of Preceding m Numbers
Minimum of Preceding m Numbers
Minimum of Preceding m Numbers
Given a sequence of n integers, for each element, compute the minimum value among the previous m numbers. If there are fewer than m numbers before the current element, consider all available previous numbers. If no number precedes the current element, output 0
.
Note: The current element is not included in the range for computing the minimum.
For example, if n = 5
, m = 3
and the sequence is [3, 2, 5, 1, 4]
then the answers are:
- For the 1st element: no preceding number, output
0
. - For the 2nd element: consider [3] → minimum is 3.
- For the 3rd element: consider [3, 2] → minimum is 2.
- For the 4th element: consider [3, 2, 5] → minimum is 2.
- For the 5th element: consider [2, 5, 1] → minimum is 1.
inputFormat
The first line contains two integers: n
(the number of elements) and m
(the number of preceding elements to consider).
The second line contains n
space-separated integers representing the sequence.
outputFormat
Output n
space-separated integers. The i
-th integer is the minimum of the previous m
numbers for the i
-th element. If there is no preceding number for an element, output 0
for that element.
sample
5 3
3 2 5 1 4
0 3 2 2 1