#FT0000204. Số nghịch thế kỳ vọng sau khi xáo trộn (Inversions After Shuffle)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Số nghịch thế kỳ vọng sau khi xáo trộn (Inversions After Shuffle)
Inversions After Shuffle
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of the integers from to .
Perform exactly one random experiment:
- Choose a contiguous segment , where . All possible segments are equally likely.
- Randomly permute the values inside , assigning equal probability to every ordering of those values. The elements outside the segment remain unchanged.
An inversion is a pair of indices with . Compute the expected number of inversions after the experiment. The required answer is a real number, not a permutation.
Input
- The first line contains the permutation length .
- The second line contains exactly integers , each integer from to appearing exactly once.
Output
Print one real number: the expected number of inversions. An absolute or relative error at most is accepted. You may print enough decimal digits to meet this precision.
Subtasks
- Subtask 1 (20 points): ; is a permutation of ; absolute or relative error at most .
- Subtask 2 (30 points): ; is a permutation of ; absolute or relative error at most .
- Subtask 3 (50 points): ; is a permutation of ; absolute or relative error at most .
Examples
Input
3
2 3 1
Output
1.916666666666666666666666666667
Explanation
For , initially the pairs and are inversions, so there are two.
There are six equally likely segments. Each of the three length-one segments keeps the inversion count at . Segment gives expectation . Segment replaces one internal inversion by an expected , giving . Segment gives an expected per pair of positions, or in total.
The six expectations add to . Dividing by six gives , matching the sample output.