Morning assembly

0

0 votes
Problem

In a local school, N students have assembled for the morning assembly. All the students are standing in a straight line. Due to some technical issues, the mic is not working. The technician is trying to solve the issue. Students have to stand in the line, all this while. After a while, students start looking at each other. But, since they are not standing in any particular order of heights, not everyone can see everyone else.

Two students X and Y can see each other if they are standing next to each other or if no student standing between them has height greater than height of either X or Y.

You will be given height of students standing in the assembly line. You have to print the number of pairs of students that can see each other.

 

Input format:-

The first line of input contains an integer N , that denotes the number of students standing in the line.

The next line contains N space seperated integers, h[i] denoting the height of students standing in the assembly line. The heights are provided in nanometers.

 

Constraints:-

1 <= N <= 5*105

0 <= h[i] <= 109

 

Output format:-

Print single integer denoting the number of pairs of students that can see each other.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

 (2, 4), (4, 1), (4, 2), (4, 2), (4, 5), (1, 2), (2, 2), (2, 5), (2, 5), (5, 1) form the valid pairs.

Editor Image

?