• CF 258 D. Little Elephant and Broken Sorting


    D. Little Elephant and Broken Sorting

    链接

    题意:

      长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$frac{1}{2}$,求m此操作后逆序对的期望。

    分析:

      f[i][j]表示i>i的概率,每次交换的概率为$frac{1}{2}$,设交换的位置是x,y,那么$f[i][x]=frac{f[i][x]+f[i][y]}{2}$,分别是不交换和交换后的概率的和除以2。

    代码:

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<cmath>
     6 #include<cctype>
     7 #include<set>
     8 #include<queue>
     9 #include<vector>
    10 #include<map>
    11 using namespace std;
    12 typedef long long LL;
    13 
    14 inline int read() {
    15     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
    16     for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
    17 }
    18 
    19 const int N = 1005;
    20 double f[N][N];
    21 int a[N];
    22 
    23 int main() {
    24     int n = read(), m = read();
    25     for (int i = 1; i <= n; ++i) a[i] = read();
    26     for (int i = 1; i <= n; ++i) 
    27         for (int j = i + 1; j <= n; ++j) 
    28             f[i][j] = a[i] > a[j], f[j][i] = a[j] > a[i];
    29     while (m --) {
    30         int x = read(), y = read();
    31         if (x == y) continue;
    32         for (int i = 1; i <= n; ++i) {
    33             if (i == x || i == y) continue;
    34             f[i][x] = f[i][y] = (f[i][x] + f[i][y]) / 2.0;
    35             f[x][i] = f[y][i] = (f[x][i] + f[y][i]) / 2.0;
    36         }
    37         f[x][y] = f[y][x] = 0.5;
    38     }
    39     double ans = 0;
    40     for (int i = 1; i <= n; ++i) 
    41         for (int j = i + 1; j <= n; ++j) ans += f[i][j];
    42     printf("%.10lf",ans);
    43     return 0;
    44 }
  • 相关阅读:
    Python合集之文件操作(二)
    Python合集之文件操作(一)
    Python合集之异常(二)
    Python合集之异常(一)
    Python合集之模块(五)
    Visual Studio 配置额外工具 Windows Terminal 等
    CMakeList.txt
    alpha智能图像(全栈的进阶之路)
    位运算实现多状态控制
    缓存函数 memorize
  • 原文地址:https://www.cnblogs.com/mjtcn/p/10200655.html
Copyright © 2020-2023  润新知