Editorial for Expected Inversions
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Given the array of length
, define
to be the probability that
and
form an inversion (
,
). For a pair
,
is defined to be
, since there are two cases;
and
.
Using linearity of expectation, the expected number of inversions of the permutation is defined as:
In this problem, since there are prefilled positions in our array, there are cases to be considered:
Case 1:
- For all
, where
we can calculate the number of inversions directly using any range tree.
Case 2:
- Considering all pairs
where
, we define
where
is the number of positions
such that
.
Case 3:
Considering pairs
where one of
is
, more careful calculation is required. We define two sets
and
where
is the set of all positions
such that
, and
holds all positions
where
.
Start by looping through all indices in
. We seek to calculate
. For each
, consider two cases where
and
.
Let
be the set
, and Let
be the set
.
For
, the probability
is defined as:
. This is since a larger integer should be placed before
, to create an inversion.
Likewise for
, the probability
is defined as:
, and this time
should be larger than the integer placed at
to create an inversion.
Time Complexity:
Comments