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=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
  16. for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
  17. }
  18.  
  19. const int N = ;
  20. double f[N][N];
  21. int a[N];
  22.  
  23. int main() {
  24. int n = read(), m = read();
  25. for (int i = ; i <= n; ++i) a[i] = read();
  26. for (int i = ; i <= n; ++i)
  27. for (int j = i + ; 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 = ; 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 = ;
  40. for (int i = ; i <= n; ++i)
  41. for (int j = i + ; j <= n; ++j) ans += f[i][j];
  42. printf("%.10lf",ans);
  43. return ;
  44. }

CF 258 D. Little Elephant and Broken Sorting的更多相关文章

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

    Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...

  2. CodeForces 258D Little Elephant and Broken Sorting(期望)

    CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...

  3. CodeForces - 258D Little Elephant and Broken Sorting

    Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of al ...

  4. CF258D Little Elephant and Broken Sorting/AGC030D Inversion Sum 期望、DP

    传送门--Codeforces 传送门--Atcoder 考虑逆序对的产生条件,是存在两个数\(i,j\)满足\(i < j,a_i > a_j\) 故设\(dp_{i,j}\)表示\(a ...

  5. CF258D Little Elephant and Broken Sorting (带技巧的DP)

    题面 \(solution:\) 这道题主要难在考场上能否想到这个思路(即如何设置状态)(像我这样的蒟蒻就想不到呀QAQ)不过这一题确实很神奇! \(f[i][j]:\)表示第 \(a_i\) 个数比 ...

  6. CodeForces - 258D:Little Elephant and Broken Sorting(概率DP)

    题意:长度为n的排列,m次交换xi, yi,每个交换x,y有50%的概率不发生,问逆序数的期望  .n, m <= 1000 思路:我们只用维护大小关系,dp[i][j]表示位置i的数比位置j的 ...

  7. 【CF】220B Little Elephant and Array

    区间动态统计的好题. /* */ #include <iostream> #include <string> #include <map> #include < ...

  8. Noip前的大抱佛脚----赛前任务

    赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...

  9. codeforces 258D

    D. Little Elephant and Broken Sorting time limit per test 2 seconds memory limit per test 256 megaby ...

随机推荐

  1. 铁乐学python_day02-作业

    1.判断下列逻辑语句的True,False. 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 解题思路 ...

  2. layer的alert图

    layer.alert("xxx",1); 1 2 3 4 5 6 7 8 9 10 11   12 13 14 15 16 17 及以后

  3. November 19th 2016 Week 47th Saturday

    Nature didn't need an operation to be beautiful. It just was. 自然之美无需刻意而为,其本身即为美. Recently I saw seve ...

  4. 题解 P1018 【乘积最大】

    题目链接:P1018 乘积最大 题面 今年是国际数学联盟确定的"2000――世界数学年",又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面 ...

  5. Ajax请求:本地跨域的问题

    问题出现一: 1.Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-e ...

  6. [T-ARA][For you]

    歌词来源:http://music.163.com/#/song?id=33682511 作曲 : Monster Factory [作曲 : Monster Factory] 作词 : Monste ...

  7. 【cs231n】图像分类 k-Nearest Neighbor Classifier(K最近邻分类器)【python3实现】

    [学习自CS231n课程] 转载请注明出处:http://www.cnblogs.com/GraceSkyer/p/8763616.html k-Nearest Neighbor(KNN)分类器 与其 ...

  8. FFI (语言交互接口(Foreign Function Interface))

    FFI(Foreign Function Interface)是用来与其它语言交互的接口, 在有些语言里面称为语言绑定(language bindings), Java 里面一般称为 JNI(Java ...

  9. POJ 3294 Life Forms [最长公共子串加强版 后缀数组 && 二分]

    题目:http://poj.org/problem?id=3294 Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submiss ...

  10. 关于ssm框架使用mysql控制台出现警告问题

    使用MySQL时,总会时不时出现这种警告信息 警告信息:WARN: Establishing SSL connection without server's identity verification ...