Complaint Sort
InterviewTime limit1sMemory limit256 MB
Given a sequence of n values, count the strictly decreasing subsequence triples (i < j < k with a_i > a_j > a_k).
- Level
Medium6 of 10
- Topics
- Array, Combinatorics, Prefix sum, Bit manipulation
- Solved
- No attempts yet
Problem
The upper bound on the number of swaps a sorting algorithm needs is , which is easy to prove. Consider picking two out-of-order elements — a pair with but — and swapping their positions. Such an out-of-order pair is called an inversion, and there are at most inversions.
Hyeonju is a child with many complaints about the world. He is unhappy even about picking just two elements when sorting. Instead, he picks three elements with and , and rewrites their positions in the order .
Hyeonju named this method the complaint sort algorithm and now wants to find its upper bound. Write a program that counts how many triples of elements Hyeonju can pick — that is, count the number of index triples with and .
Input
The first line contains the length of the sequence. ()
The second line contains the elements of the sequence separated by spaces. Each element is an integer between and , inclusive.
Output
Print, on the first line, the number of index triples that satisfy and .