#K74927. Orderly Queue
Orderly Queue
Orderly Queue
You are given a sequence of integers representing the heights of people standing in a queue. You need to process the queue and remove some people so that every remaining person is at least as tall as every person standing before them.
More formally, given a list of integers heights
, construct a new list by choosing the first height, and then for every subsequent height in the original list, include it in the new list if and only if it is not shorter than the last height included. That is, if result
is the new list, then for each height h
(in order), if h \geq result[-1]
then append h
to result
.
For example:
- For
heights = [5, 3, 4, 6, 7, 2, 5]
, the resulting list is[5, 6, 7]
. - For
heights = [1, 2, 2, 3, 1, 4]
, the resulting list is[1, 2, 2, 3, 4]
.
You are required to implement this logic. The constraint \(n\) (the number of people) is such that \(0 \le n \le 10^5\), and the heights are within reasonable integer bounds.
inputFormat
The input consists of two lines:
- The first line contains a single integer \(n\) representing the number of people in the queue.
- If \(n > 0\), the second line contains \(n\) space-separated integers representing the heights of the people.
If \(n = 0\), there will be no second line.
outputFormat
Output a single line containing the resulting list of heights after processing, with each height separated by a space. If the resulting list is empty, output nothing.
## sample7
5 3 4 6 7 2 5
5 6 7