Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp

时间:2022-10-01 08:02:37

Little Elephant and Broken Sorting

怎么感觉这个状态好难想到啊。。

dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率。转移好像比较显然。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, m, a[N];
double dp[N][N]; int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(a[i] > a[j]) dp[i][j] = ;
while(m--) {
int a, b; scanf("%d%d", &a, &b);
for(int i = ; i <= n; i++) {
if(i == a || i == b) continue;
dp[i][a] = dp[i][b] = (dp[i][a] + dp[i][b]) / ;
dp[a][i] = dp[b][i] = (dp[a][i] + dp[b][i]) / ;
}
dp[a][b] = dp[b][a] = 0.5;
}
double ans = ;
for(int i = ; i <= n; i++)
for(int j = i + ; j <= n; j++)
ans += dp[i][j];
printf("%.12f\n", ans);
return ;
} /*
*/