Description
有一个 1 ∼ n 1 \sim n 1∼n的排列,会进行m次操作,操作为交换a,b。每次操作都有 50 % 50\% 50%的概率进行。
求进行m次操作以后的期望逆序对个数。
n
,
m
≤
1000
n,m \le 1000
n,m≤1000
Translated by Youngsc
Solution
bzoj权限号到期了gg,心态崩了.jpg
记f[i,j]为a[i]>a[j]的概率,那么答案实际上就是
∑
i
=
1
n
∑
j
=
i
+
1
n
f
[
i
,
j
]
\sum\limits_{i=1}^{n}\sum\limits_{j=i+1}^{n}f[i,j]
i=1∑nj=i+1∑nf[i,j]
初始的f全都是1,然后每次改的时候直接算就可以了
Code
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
typedef double db;
const int N=2005;
db f[N][N],r[N];
int a[N];
int main(void) {
int n,m; scanf("%d%d",&n,&m);
rep(i,1,n) scanf("%d",&a[i]);
rep(i,1,n) rep(j,1,n) f[i][j]=(a[i]>a[j]);
for (;m--;) {
int x,y; scanf("%d%d",&x,&y);
if (x==y) continue;
rep(i,1,n) if (i!=x&&i!=y) {
r[i]=f[x][i];
f[x][i]=(f[x][i]+f[y][i])*0.5;
f[i][x]=1-f[x][i];
}
rep(i,1,n) if (i!=x&&i!=y) {
f[y][i]=(r[i]+f[y][i])*0.5;
f[i][y]=1-f[y][i];
}
f[x][y]=f[y][x]=0.5;
}
db ans=0;
rep(i,2,n) rep(j,1,i-1) ans+=f[j][i];
printf("%.8lf\n", ans);
return 0;
}