#P8600. Counting Consecutive Intervals in Permutations

    ID: 21766 Type: Default 1000ms 256MiB

Counting Consecutive Intervals in Permutations

Counting Consecutive Intervals in Permutations

Consider a permutation of integers from 1 to N. An interval [L, R] (1-indexed) is called a consecutive interval if, after sorting the elements in the range, they form a sequence of consecutive integers. In mathematical terms, let the subsequence be \(a_L, a_{L+1}, \dots, a_R\) and denote \(S = \{a_L, a_{L+1}, \dots, a_R\}\). Then the interval is consecutive if and only if

[ \max(S) - \min(S) = R - L ]

Your task is to count the number of consecutive intervals in the given permutation.

inputFormat

The first line contains an integer N, the size of the permutation.
The second line contains N space-separated integers representing the permutation.

outputFormat

Output a single integer, which is the count of consecutive intervals.

sample

1
1
1