Little Elephant and Broken Sorting (Codeforces 258D)
Tags: Math, probabilities

The Little Elephant loves permutations of integers from 1 to n very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1, 2, 3, ..., n.This time the Little Elephant has permutation p1, p2, ..., pn. Its sorting program needs to make exactly m moves, during the i-th move it swaps elements that are at that moment located at the ai-th and the bi-th positions. But the Little Elephant's sorting program happened to break down and now on every step it can equiprobably either do nothing or swap the required elements.Now the Little Elephant doesn't even hope that the program will sort the permutation, but he still wonders: if he runs the program and gets some permutation, how much will the result of sorting resemble the sorted one? For that help the Little Elephant find the mathematical expectation of the number of permutation inversions after all moves of the program are completed.We'll call a pair of integers i, j (1 ≤ i < j ≤ n) an inversion in permutatuon p1, p2, ..., pn, if the following inequality holds: pi > pj.

Input: The first line contains two integers n and m (1 ≤ n, m ≤ 1000, n > 1) — the permutation size and the number of moves. The second line contains n distinct integers, not exceeding n — the initial permutation. Next m lines each contain two integers: the i-th line contains integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the positions of elements that were changed during the i-th move.

Output: In the only line print a single real number — the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed 10 - 6.


Sample Inputs:
2 1
1 2
1 2

4 3
1 3 2 4
1 2
2 3
1 4

Sample Outputs:
0.500000000

3.000000000

Solution (Python):
(n, m) = map(int, input().split())
p = [0] + list(map(int, input().split()))
dp = [[0] * (n + 1) for i in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, n + 1):
        if p[i] > p[j]:
            dp[i][j] = 1
ans = 0
for _ in range(m):
    (x, y) = map(int, input().split())
    for i in range(1, n + 1):
        dp[i][x] = dp[i][y] = 0.5 * (dp[i][x] + dp[i][y])
        dp[x][i] = dp[y][i] = 1 - dp[i][x]
    dp[x][y] = dp[y][x] = 0.5
for i in range(1, n):
    for j in range(i + 1, n + 1):
        ans += dp[i][j]
print(ans)

Solution (Python):
inp = input().split(' ')
val = []
totNums = int(inp[0])
totOpt = int(inp[1])
inp = input().split(' ')
for it in inp:
    val.append(int(it))
dp = [[0.0 for _ in range(0, totNums)] for __ in range(0, totNums)]
for i in range(0, totNums):
    for j in range(0, totNums):
        if val[i] > val[j]:
            dp[i][j] = 1.0
while totOpt > 0:
    totOpt -= 1
    inp = input().split(' ')
    fr = int(inp[0]) - 1
    to = int(inp[1]) - 1
    for i in range(0, totNums):
        if i != fr and i != to:
            dp[i][fr] = dp[i][to] = (dp[i][fr] + dp[i][to]) / 2
            dp[fr][i] = dp[to][i] = (dp[fr][i] + dp[to][i]) / 2
    dp[fr][to] = dp[to][fr] = (dp[fr][to] + dp[to][fr]) / 2
ans = 0.0
for i in range(0, totNums):
    for j in range(i + 1, totNums):
        ans += dp[i][j]
print('%.10f' % ans)

Solution (Python):
(N, Q) = map(int, input().split())
A = list(map(int, input().split()))
dp = [[0] * N for i in range(N)]
for i in range(N):
    for j in range(N):
        if A[i] < A[j]:
            dp[i][j] = 1
for _ in range(Q):
    (X, Y) = map(int, input().split())
    X -= 1
    Y -= 1
    dct = {}
    for i in range(N):
        if i in (X, Y):
            continue
        x = (dp[i][X] + dp[i][Y]) / 2
        (dp[i][X], dp[i][Y]) = (x, x)
        x = (dp[X][i] + dp[Y][i]) / 2
        (dp[X][i], dp[Y][i]) = (x, x)
    x = (dp[X][Y] + dp[Y][X]) / 2
    (dp[X][Y], dp[Y][X]) = (x, x)
ans = 0
for i in range(N):
    for j in range(i):
        ans += dp[i][j]
print(ans)