Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp
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 = 1000 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); 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 < 0) 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 = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(a[i] > a[j]) dp[i][j] = 1; while(m--) { int a, b; scanf("%d%d", &a, &b); for(int i = 1; i <= n; i++) { if(i == a || i == b) continue; dp[i][a] = dp[i][b] = (dp[i][a] + dp[i][b]) / 2; dp[a][i] = dp[b][i] = (dp[a][i] + dp[b][i]) / 2; } dp[a][b] = dp[b][a] = 0.5; } double ans = 0; for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) ans += dp[i][j]; printf("%.12f\n", ans); return 0; } /* */