PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000232. Nghịch thế toàn cục và cục bộ (Global and Local Inversions)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 aa of 0,1,…,n−10,1,\ldots,n-1, where every number appears exactly once. Array positions are indexed from 00 to n−1n-1.

A global inversion is a pair (i,j)(i,j) satisfying 0≤i<j<n0\le i<j<n and ai>aja_i>a_j. A local inversion is a pair of adjacent positions (i,i+1)(i,i+1) satisfying 0≤i<n−10\le i<n-1 and ai>ai+1a_i>a_{i+1}.

Determine whether the number of global inversions equals the number of local inversions.

Input

  • The first line contains the integer nn.
  • The second line contains exactly nn integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1} in their original order.

Output

Print true if the two counts are equal; otherwise print false (lowercase).

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le 10; 0≤ai<n0\le a_i<n and aa is a permutation of 0,…,n−10,\ldots,n-1.

  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 0≤ai<n0\le a_i<n and aa is a permutation of 0,…,n−10,\ldots,n-1.

  • Subtask 3 (50 points): 1≤n≤1051\le n\le 10^5; 0≤ai<n0\le a_i<n and aa is a permutation of 0,…,n−10,\ldots,n-1.

Examples

Input

3
1 0 2

Output

true

Explanation

For [1,0,2][1,0,2], the only global inversion is positions (0,1)(0,1) because 1>01>0. These positions are also adjacent, so there is exactly one local inversion. Both counts are 11 and the answer is true.