using
System;
using
System.Collections.Generic;
class
GFG
{
public
static
Dictionary<List<
int
>,
double
> memo =
new
Dictionary<List<
int
>,
double
>();
public
static
bool
checkEquality(List<
int
> a,List<
int
> b){
if
(a.Count != b.Count)
return
false
;
for
(
int
i = 0 ; i < a.Count ; i++){
if
(a[i]!=b[i])
return
false
;
}
return
true
;
}
public
static
double
containsKey(List<
int
> a){
foreach
(KeyValuePair<List<
int
>,
double
> x
in
memo){
if
(checkEquality(a, x.Key)){
return
x.Value;
}
}
return
-1;
}
public
static
double
expectedSwaps(List<
int
> a)
{
double
res = containsKey(a);
if
(res != -1) {
return
res;
}
res = 0;
int
K = 0;
for
(
int
i = 0 ; i < a.Count ; i++) {
for
(
int
j = i + 1 ; j < a.Count ; j++) {
if
(a[i] > a[j]) {
int
temp = a[i];
a[i] = a[j];
a[j] = temp;
res += 1 + expectedSwaps(a);
temp = a[i];
a[i] = a[j];
a[j] = temp;
K++;
}
}
}
if
(K == 0)
res = 0;
else
res = res/(
double
)K;
memo[
new
List<
int
>(a)] = res;
return
res;
}
public
static
void
Main(
string
[] args){
List<
int
> arr =
new
List<
int
>{ 3, 2, 1 };
Console.Write(expectedSwaps(arr));
}
}