Problem 819
Global and Local Inversions
Problem
The array is a permutation of 0 to N - 1. A global inversion is a pair i < j with A[i] > A[j]; a local inversion is an index i with A[i] > A[i + 1]. Every local inversion is also global, so the counts are equal exactly when there is no global inversion between elements that are two or more positions apart. That holds exactly when every element is at most one position away from its sorted place: |A[i] - i| <= 1.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1]. The values are a permutation of 0 to N - 1.
Output
Print YES if the numbers of global and local inversions are equal, otherwise NO.
Constraints
1 <= N <= 100000; A is a permutation of 0 .. N-1
Example input
3 1 0 2
Example output
YES
Explanation
1 0 2 has one global inversion (1 > 0) and one local inversion (at index 0), so the answer is YES. 1 2 0 has two global inversions but one local, so it is NO.