CF 258 D. Little Elephant and Broken Sorting

时间:2023-03-09 00:29:56
CF 258 D. Little Elephant and Broken Sorting

D. Little Elephant and Broken Sorting

链接

题意:

  长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$\frac{1}{2}$,求m此操作后逆序对的期望。

分析:

  f[i][j]表示i>i的概率,每次交换的概率为$\frac{1}{2}$,设交换的位置是x,y,那么$f[i][x]=\frac{f[i][x]+f[i][y]}{2}$,分别是不交换和交换后的概率的和除以2。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
double f[N][N];
int a[N]; int main() {
int n = read(), m = read();
for (int i = ; i <= n; ++i) a[i] = read();
for (int i = ; i <= n; ++i)
for (int j = i + ; j <= n; ++j)
f[i][j] = a[i] > a[j], f[j][i] = a[j] > a[i];
while (m --) {
int x = read(), y = read();
if (x == y) continue;
for (int i = ; i <= n; ++i) {
if (i == x || i == y) continue;
f[i][x] = f[i][y] = (f[i][x] + f[i][y]) / 2.0;
f[x][i] = f[y][i] = (f[x][i] + f[y][i]) / 2.0;
}
f[x][y] = f[y][x] = 0.5;
}
double ans = ;
for (int i = ; i <= n; ++i)
for (int j = i + ; j <= n; ++j) ans += f[i][j];
printf("%.10lf",ans);
return ;
}