CF258D - Little Elephant and Broken Sorting(dp)
题目
有一个长度为\(n\)的排列\(a\),\(m\)次操作交换顺序执行,操作\((i,j)\)代表交换\(a_i,a_j\)的值。每次操作等概率发生或不发生。问\(m\)次操作后逆序数对的期望是多少。
题解
经典的转求贡献的问题。只要定义好dp状态就很容易。设\(dp[i][j]\)代表\(a_i>a_j\)的概率。然后转移方程顺带就出来了。最后答案为
\[\sum\limits_{i<j}{dp[i][j]}
\]
时间复杂度\(O(n^2)\)
#include <bits/stdc++.h>
#define endl '\n'
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mp make_pair
#define seteps(N) fixed << setprecision(N)
typedef long long ll;
using namespace std;
/*-----------------------------------------------------------------*/
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return (__int128)a * b / gcd(a, b);}
#define INF 0x3f3f3f3f
const int N = 3e3 + 10;
const double eps = 1e-5;
double f[N][N];
double g[N][N];
int arr[N];
int main() {
IOS;
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> arr[i];
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
f[i][j] = (arr[i] > arr[j]);
}
}
while(m--) {
int a, b;
cin >> a >> b;
for(int i = 1; i <= n; i++) {
if(i == a || i == b) continue;
g[i][a] = 0.5 * f[i][b] + 0.5 * f[i][a];
g[a][i] = 0.5 * f[b][i] + 0.5 * f[a][i];
g[i][b] = 0.5 * f[i][a] + 0.5 * f[i][b];
g[b][i] = 0.5 * f[a][i] + 0.5 * f[b][i];
}
f[a][b] = 0.5 * f[a][b] + 0.5 * (1 - f[a][b]);
f[b][a] = 0.5 * f[b][a] + 0.5 * (1 - f[b][a]);
for(int i = 1; i <= n; i++) {
if(i == a || i == b) continue;
f[i][a] = g[i][a];
f[a][i] = g[a][i];
f[i][b] = g[i][b];
f[b][i] = g[b][i];
}
}
double ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i >= j) continue;
ans += f[i][j];
}
}
cout << seteps(10) << ans << endl;
}