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。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
double f[N][N];
int a[N]; int main() {
int n = read(), m = read();
for (int i = ; i <= n; ++i) a[i] = read();
for (int i = ; i <= n; ++i)
for (int j = i + ; j <= n; ++j)
f[i][j] = a[i] > a[j], f[j][i] = a[j] > a[i];
while (m --) {
int x = read(), y = read();
if (x == y) continue;
for (int i = ; i <= n; ++i) {
if (i == x || i == y) continue;
f[i][x] = f[i][y] = (f[i][x] + f[i][y]) / 2.0;
f[x][i] = f[y][i] = (f[x][i] + f[y][i]) / 2.0;
}
f[x][y] = f[y][x] = 0.5;
}
double ans = ;
for (int i = ; i <= n; ++i)
for (int j = i + ; j <= n; ++j) ans += f[i][j];
printf("%.10lf",ans);
return ;
}

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. 精简计算UITableView文本高度

    精简计算UITableView文本高度 本人视频教程系类   iOS中CALayer的使用 最终效果: 核心源码(计算文本高度的类) NSString+StringHeight.h 与 NSStrin ...

  2. Man's Best Friend: The Science Behind the Dog and Human Relationship

    http://info.thinkfun.com/stem-education/mans-best-friend-the-science-behind-the-dog-and-human-relati ...

  3. Linux echo命令详解

    echo :输出文字到控制台 -n: 不换行输出 -e:解析转移字符   (-b: 退格  -n 换行 -t 空格) 常用的命令展示 echo {1..4} ==> seq -s " ...

  4. PHP防SQL注入和XSS攻击

    摘要: 就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.在用户名输入框中输入:' or 1=1#,密码随便输入,这时候的合成后的SQL ...

  5. wordpress利用rsync同步备份

    我搭建的wordpress服务器现在使用的系统是opensuse, 服务器上面已做脚本和计划任务将wordpress使用的数据库与web目录每天压缩备份放到 /data/backup目录下 opera ...

  6. nginx过一段时间出现400 Bad Request 错误的解决方法

    tomcat整合nginx成功后,等访问一段时间后,会出现 Bad Request (Invalid Hostname)的错误, 因为是已经成功的配置,所以判定可能是哪里的限制设置有问题,最后在官方网 ...

  7. Oracle 关于WKT构造SDO_GEOMETRY的问题。

    由于系统前端使用OpenLayers框架,后台数据库使用oracle spatial.大家知道Oracle spatial的SDO_GEOMETRY十分复杂,如果使用期java api ,那就坑爹了, ...

  8. ElasticSearch学习之——基本的文档CURD

    一.文档的添加 POST http://127.0.0.1:9200/{index}/{type}/{id} { "key":"value", "ke ...

  9. Day13 泛型

    泛型 泛型定义 在一个类型(类,接口,方法)之后,定义一个类型参数. 原生类型:类型后面没有指定具体的类型参数. 好处 使用泛型的好处在于,它在编译的时候进行类型安全检查,并且在运行时所有的转换都是强 ...

  10. python 内置常用函数

    import os def set(o): return set(o) # =={o} def reverseObject(it): it.reverse() return it def sortOb ...