Dividers
InterviewTime limit1sMemory limit128 MB
Count the positions where the first k entries hold exactly 1 to k by tracking the running maximum.
Problem
All integers from to are written in a single row in some order, forming a sequence. This sequence is a permutation of the numbers through .
You want to insert as many dividers into this sequence as possible. A divider can be placed between two adjacent elements of the sequence (or at the very end).
You may place a divider right after the -th element only if the first elements contain every number from to exactly once. In other words, the first elements must equal the set . In particular, a divider can always be placed right after the -th element, because the whole sequence is a permutation of through .
Find the maximum number of dividers you can insert.
Input
The first line contains an integer (), the length of the sequence.
The second line contains a permutation () of the numbers through , separated by spaces, where is the -th number in the sequence.
Output
Print a single integer: the maximum number of dividers that can be inserted.
Hint
For example, when the sequence is 2 1 3 6 5 4 9 10 8 7, the dividers can be placed as follows: 2 1 | 3 | 6 5 4 | 9 10 8 7 |. In this case there are dividers in total.