#C5640. Unobstructed Building Views

    ID: 49312 Type: Default 1000ms 256MiB

Unobstructed Building Views

Unobstructed Building Views

You are given a row of buildings where the i-th building has a height given by an integer. A building has an unobstructed view to the right if there is no building on its right that is taller than it. In other words, a building can see the sunset if all the buildings to its right are not taller than it.

Your task is to determine the number of buildings that have an unobstructed view to the right.

Examples:

  • For heights = [5, 5, 5, 5, 5], only the last building has a view, so the output is 1.
  • For heights = [1, 2, 3, 4, 5], only the last building can see, hence the output is 1.
  • For heights = [5, 4, 3, 2, 1], every building has a view, so the output is 5.
  • For heights = [3, 7, 8, 3, 6, 1], the buildings with heights 8, 6, and 1 have views, giving an output of 3.

The formula to determine if a building at index i has an unobstructed view can be expressed as follows:

\[ \text{view}(i) = \begin{cases} 1 & \text{if } h_i > \max\{h_{i+1}, h_{i+2}, \ldots, h_n\} \\ 0 & \text{otherwise} \end{cases} \]

Where \(h_i\) is the height of the i-th building and \(n\) is the total number of buildings.

inputFormat

The input is read from stdin and consists of two lines:

  1. The first line contains a single integer \(n\) indicating the number of buildings.
  2. The second line contains \(n\) space-separated integers representing the heights of the buildings.

outputFormat

Output a single integer to stdout which is the count of buildings that have an unobstructed view to the right.

## sample
5
5 5 5 5 5
1