#K40972. Counting Visible Trees
Counting Visible Trees
Counting Visible Trees
You are given a row of trees, each with a specific height. The trees are arranged from left to right. From a certain viewpoint located on the right end of the row, a tree is visible if there is no tree taller than it to its right. In other words, starting from the rightmost tree, every tree that is taller than all trees seen so far (to its right) is visible.
Your task is to compute the number of trees that are visible from the right side.
The problem can be formulated mathematically as follows:
Let \( h_1, h_2, \dots, h_n \) represent the heights of the trees from left to right. A tree \( h_i \) is visible if \( h_i > \max(h_{i+1}, h_{i+2}, \dots, h_{n}) \). The rightmost tree is always visible.
Example:
For n = 5 and heights = [2, 5, 3, 4, 1], the visible trees are those with heights 1, 4, and 5, so the answer is 3.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a single integer \( n \) representing the number of trees.
- The second line contains \( n \) integers separated by spaces, where each integer represents the height of a tree.
outputFormat
Output a single integer to the standard output (stdout) which is the number of trees visible from the right side.
## sample1
5
1
</p>