#STK0000079. Cuộc hội ngộ Oasis (Oasis Reunion)
Cuộc hội ngộ Oasis (Oasis Reunion)
Oasis Reunion
Source: Baekjoon Online Judge
Version: Phuoc Hung OJ Extended
Problem Statement
There are people standing in a line, each with a height. Two people can see each other if every person between them is no taller than the shorter of the two endpoints. Count the number of pairs that can see each other.
Input
The first line contains . Each of the next lines contains one person height in queue order.
Output
Print the number of pairs of people that can see each other.
Subtasks
- Subtask 1 (20 points): ; all other conditions are unchanged.
- Subtask 2 (30 points): and all heights are pairwise distinct.
- Subtask 3 (50 points): , .
Examples
Input
7
2
4
1
2
2
5
1
Output
10
Explanation
Number the people from to ; their heights are .
The visible pairs are:
(1,2), (2,3), (2,4), (2,5), (2,6), (3,4), (4,5), (4,6), (5,6), (6,7).
For example, people and have heights and . The intermediate heights are and , neither of which exceeds the shorter endpoint height , so this pair is visible. In contrast, people and are not visible to each other because person between them has height , which exceeds the shorter endpoint.
There are exactly valid pairs, so the program prints 10.