Random Sort 2
Time limit2sMemory limit512 MB
Compute the expected number of random swaps the process needs to turn a given permutation of size up to 10 into increasing order.
- Level
Medium7 of 10
- Topics
- Probability, Dynamic programming, Math, Matrix
- Solved
- No attempts yet
Problem
Random sort is a way to sort a permutation of size with the algorithm below.
function random_sort(permutation P) {
swaps = 0;
while (not sorted P) {
(i, j) = random pair (1 <= i < j <= N)
swap(P[i], P[j])
swaps = swaps + 1;
}
return swaps;
}
random pair picks one of the pairs that satisfy , uniformly at random and independently on every iteration. The loop stops once is in increasing order, and the function returns how many swaps it counted.
Given a permutation , write a program that computes the expected return value of random_sort.
Input
The first line contains the size of the permutation ().
The second line contains the permutation , separated by spaces. Every integer from to appears exactly once.
Output
Print the expected return value of random_sort with exactly seven digits after the decimal point. Round half up at the eighth digit and pad with zeros when digits are left over. If the given permutation is already in increasing order, print 0.0000000.