#FT0000106. Đếm bộ bốn tăng đan xen (Count Increasing Quadruplets)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đếm bộ bốn tăng đan xen (Count Increasing Quadruplets)
Count Increasing Quadruplets
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of the integers from to . Every integer occurs exactly once, and positions are numbered from through .
Consider any quadruple of indices satisfying . The quadruple is valid exactly when the values at these indices satisfy
Thus the values do not need to be increasing in index order: in particular, the value at the third selected position must be smaller than the value at the second selected position, even though .
Count all valid quadruples of indices. Two quadruples are different if at least one index differs. The answer may exceed the range of a 32-bit signed integer.
Input
- The first line contains the integer , the length of the permutation.
- The second line contains exactly integers , in index order from through .
Output
Print one integer: the number of quadruples satisfying both and .
Subtasks
- Subtask 1 (20 points): ; is a permutation of (every value occurs exactly once).
- Subtask 2 (30 points): ; is a permutation of (every value occurs exactly once).
- Subtask 3 (50 points): ; is a permutation of (every value occurs exactly once).
Examples
Example 1
Input
5
1 3 2 4 5
Output
2
Explanation
The only qualifying middle pair is , with values . Index contributes value . We may choose either (value ) or (value ); both exceed . Thus exactly and qualify, giving 2.
Example 2
Input
4
1 2 3 4
Output
0
Explanation
The only quadruple of indices is , but whereas the required condition is . Therefore the answer is 0.