#P6854. Beautiful Photo Segments
Beautiful Photo Segments
Beautiful Photo Segments
You are about to debut! Your coach and teammates have arranged a photographer for your promotional photoshoot along De Forest Road. There are n buildings in a row on the street, numbered from 1 to n from left to right, where the height of the i-th building is (a_i).
A photo is represented by a tuple ((l, r)), meaning it includes all buildings with indices in the interval ([l, r]) ((1 \le l \le r \le n)). The photographer considers a photo to be beautiful if and only if it satisfies both of the following conditions:
- For any three integers (i < j < k), if there is at least one building of height (i) and at least one building of height (k) in the photo, then there must also be at least one building of height (j) in the photo. Equivalently, the set of distinct building heights present in the photo must form a consecutive interval of integers.
- For every integer (i), buildings of height (i) appear either 0 times or exactly (i) times in the photo.
Two photos ((l_1, r_1)) and ((l_2, r_2)) are considered different if (l_1 \neq l_2) or (r_1 \neq r_2).
Given the sequence of building heights, count how many different beautiful photos can be taken.
inputFormat
The first line contains an integer (n) (the number of buildings).
The second line contains (n) space-separated integers (a_1, a_2, \dots, a_n) representing the heights of the buildings.
outputFormat
Output a single integer, the number of beautiful photos (segments) that can be taken.
sample
3
1 2 3
1