• 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
    ", ans);
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    学习新东西 方法
    转 Dock 外 命令解析
    转 Dockerfile 常用指令
    RPC应用的java实现(转)
    link with editor
    org.xml.sax.SAXParseException: prolog 中不允许有内容
    webservice
    logging.xml file setfile(null,true) call failed
    log4j配置 logging.xml (转载)
    tomcat server.xml docbase workdir
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10752562.html
Copyright © 2020-2023  润新知