#FT0000060. Đếm nghịch thế của dãy phân biệt (Inversion Count)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đếm nghịch thế của dãy phân biệt (Inversion Count)
Inversion Count
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a sequence of pairwise distinct positive integers, indexed from to .
An inversion is a pair for which position precedes but , that is, and .
Calculate the total number of inversions. The answer may exceed the range of a signed 32-bit integer. The relative order in the given sequence matters and must be preserved when counting.
Input
The first line contains .
Exactly integers follow in positional order; whitespace or line breaks may separate them.
Output
Print one integer: the number of inversions.
Subtasks
-
Subtask 1 (20 points): ; for every element; all values are pairwise distinct.
-
Subtask 2 (30 points): ; for every element; all values are pairwise distinct.
-
Subtask 3 (50 points): ; for every element; all values are pairwise distinct.
Examples
Input
5
2
3
8
6
1
Output
5
Explanation
The sequence is . The inversion position pairs are for , for , for , for , and for .
These are exactly the five qualifying pairs, so the output is 5.