#FT0000232. Nghịch thế toàn cục và cục bộ (Global and Local Inversions)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Nghịch thế toàn cục và cục bộ (Global and Local Inversions)
Global and Local Inversions
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of , where every number appears exactly once. Array positions are indexed from to .
A global inversion is a pair satisfying and . A local inversion is a pair of adjacent positions satisfying and .
Determine whether the number of global inversions equals the number of local inversions.
Input
- The first line contains the integer .
- The second line contains exactly integers in their original order.
Output
Print true if the two counts are equal; otherwise print false (lowercase).
Subtasks
-
Subtask 1 (20 points): ; and is a permutation of .
-
Subtask 2 (30 points): ; and is a permutation of .
-
Subtask 3 (50 points): ; and is a permutation of .
Examples
Input
3
1 0 2
Output
true
Explanation
For , the only global inversion is positions because . These positions are also adjacent, so there is exactly one local inversion. Both counts are and the answer is true.