问题3442--Mega Inversions

3442: Mega Inversions

[命题人 : ]
时间限制 : 5.000 sec  内存限制 : 640 MB

题目描述

The n2 upper bound for any sorting algorithm is easy to obtain: just take two elements that are misplaced with respect to each other and swap them. Conrad conceived an algorithm that proceeds by taking not two, but three misplaced elements. That is, take three elements ai > aj > ak with i < j < k and place them in order ak; aj ; ai. Now if for the original algorithm the steps are bounded by the maximum number of inversions n(n-1)/2 , Conrad is at his wits' end as to the upper bound for such triples in a given sequence. He asks you to write a program that counts the number of such triples.

输入

The first line of the input is the length of the sequence, 1 <= n <= 105. The next line contains the integer sequence a1,a2... an. You can assume that all ai ∈[1, n].

输出

Output the number of inverted triples.

样例输入 Copy

3
1 2 3
4
3 3 2 1

样例输出 Copy

0
2

来源/分类