Sort
Time limit2sMemory limit128 MB
For an array of at most 8 elements, compute the expected number of random-swap steps until sorted for two different swap schemes.
- Level
Hard8 of 10
- Topics
- Probability, Dynamic programming, Simulation
- Solved
- No attempts yet
Problem
Mirko and Slavko, two rising stars of computer science, often pass the time by inventing new algorithms. Right now they are playing with sorting. Mirko proposed the following algorithm:
while (!sorted(A)) {
int i = random(N);
int j = random(N);
if (A[min(i,j)] > A[max(i,j)])
swap(A[i], A[j]);
}
Inspired by Mirko's version, Slavko proposed this one:
while (!sorted(A)) {
int i = random(N-1);
int j = i + 1;
if (A[i] > A[j])
swap(A[i], A[j]);
}
The function random(k) returns an integer from , and every integer is equally likely. sorted(A) is true when is in non-decreasing order.
Now they want to know which algorithm is better. Given an array of length , find the expected number of steps each algorithm needs to finish. One step is one complete iteration of the while loop.
Input
The first line contains the number of elements of the array ().
The second line contains the elements of the array, separated by spaces ().
Output
On the first line, print the expected number of steps of Mirko's algorithm. On the second line, print the expected number of steps of Slavko's algorithm.
Round each exact expected value to six digits after the decimal point and print exactly six digits after the decimal point. For example, if the expected value is , print 14.375000.