RandomSort
SRM 402 · 2008-05-24 · by srbga
Problem Statement
You are given a
Notes
- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
- permutation will contain between 1 and 8 elements, inclusive.
- permutation will contain each integer between 1 and n, inclusive, exactly once, where n is the number of elements in permutation.
{1,3,2}
Returns: 1.0
Exactly one swap is needed.
{4,3,2,1}
Returns: 4.066666666666666
In the first step, any two elements can be swapped.
{1}
Returns: 0.0
This permutation is already sorted, so there's no need to perform any swaps.
{2,5,1,6,3,4}
Returns: 5.666666666666666
{1,2}
Returns: 0.0
{8,7,6,5,4,3,2,1}
Returns: 13.221053196152711
Maximal number of inversions.
{8,7,6,5,1,4,3,2}
Returns: 12.895467847303319
Almost maximal number of inversions.
{4, 1, 2, 3}
Returns: 3.0
3 inversions; 1, 1, or 1 inversion removed.
{1, 4, 3, 2}
Returns: 2.333333333333333
3 inversions; 3, 1, or 1 inversion removed.
{2, 1, 4, 3, 6, 5, 8, 7}
Returns: 4.0
Maximal number of pairwise independent inversions.
{4, 3, 2, 1, 8, 7, 6, 5}
Returns: 8.133333333333333
Two independent 10-inversion groups.
{1, 2, 3, 4, 5, 6, 7, 8}
Returns: 0.0
Longest test with no inversions.
Submissions are judged against all 137 archived test cases, of which 12 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class RandomSort with a public method double getExpected(vector<int> permutation) · 137 test cases · 2 s / 256 MB per case