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。
代码:
#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的更多相关文章
- Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp
Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...
- CodeForces 258D Little Elephant and Broken Sorting(期望)
CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...
- 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 ...
- CF258D Little Elephant and Broken Sorting/AGC030D Inversion Sum 期望、DP
传送门--Codeforces 传送门--Atcoder 考虑逆序对的产生条件,是存在两个数\(i,j\)满足\(i < j,a_i > a_j\) 故设\(dp_{i,j}\)表示\(a ...
- CF258D Little Elephant and Broken Sorting (带技巧的DP)
题面 \(solution:\) 这道题主要难在考场上能否想到这个思路(即如何设置状态)(像我这样的蒟蒻就想不到呀QAQ)不过这一题确实很神奇! \(f[i][j]:\)表示第 \(a_i\) 个数比 ...
- CodeForces - 258D:Little Elephant and Broken Sorting(概率DP)
题意:长度为n的排列,m次交换xi, yi,每个交换x,y有50%的概率不发生,问逆序数的期望 .n, m <= 1000 思路:我们只用维护大小关系,dp[i][j]表示位置i的数比位置j的 ...
- 【CF】220B Little Elephant and Array
区间动态统计的好题. /* */ #include <iostream> #include <string> #include <map> #include < ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
- codeforces 258D
D. Little Elephant and Broken Sorting time limit per test 2 seconds memory limit per test 256 megaby ...
随机推荐
- 关于解决sql2012编辑器对象名无效问题
出现以下情况: 解决办法: 选择“编辑”——“Intellisense”——“刷新本地缓存” 或者按Ctrl+Shift+R组合键
- c++与matlab联合编程,调用Deploytool 生成exe文件和dll文件(转)
转自:http://www.cnblogs.com/xlw1219/archive/2012/12/25/2832222.html 首先必须知道联合编程需要知道的一些命令解释: mcc 的作用是将 . ...
- 【2017.12.05 智能驾驶/汽车电子】转载:如何成为一名无人驾驶工程师 By刘少山
之前对无人驾驶的理解就是通过刘少山老师的书:第一本无人驾驶技术书 通读之后,对智能驾驶有了一个初步的认识,如感知.决策.控制都涉及哪些领域,有哪些可以利用的技术: 但经过一段时间的实践,发现即使是在我 ...
- 工作中碰到的一个问题(cookie相关)
今天上线了一个API,6台机器做的集群.API的第一步是读取cookie,判断用户是否登录. 例如,线上服务器分别是 10.255.242.1 10.255.242.2 10.255.242.3 10 ...
- [Java多线程] LinkedBlockingQueue
java.util.concurrent包下的新类.LinkedBlockingQueue就是其中之一,是一个阻塞的线程安全的队列,底层采用链表实现. LinkedBlockingQueue Link ...
- php魔术变量
__LINE__ 文件中的当前行号 __FILE__ 文件的完整路径和文件名 __DIR__ 文件所在的目录 __FUNCTION__ 自 PHP 5 起本常量返回该函数被定义时的名字 __C ...
- HBase学习之路 (十)HBase表的设计原则
建表高级属性 下面几个 shell 命令在 hbase 操作中可以起到很大的作用,且主要体现在建表的过程中,看 下面几个 create 属性 1. BLOOMFILTER 默认是 NONE 是否使用布 ...
- python+requests实现接口测试 - cookies的使用
在很多时候,发送请求后,服务端会对发送请求方进行身份识别,如果请求中缺少识别信息或存在错误的识别信息, 会造成识别失败. 如一些需要用户登录以后才能访问的页面. import requests mya ...
- JS判断指定dom元素是否在屏幕内的方法实例
前言 刷网页的时候,有时会遇到这样一个情景,当某个dom元素滚到可见区域时,或者图片的懒加载效果,它就会展现显示动画,十分有趣.那么这是如何实现的呢? 实现原理 想要实现这个功能,就要知道具体的实现原 ...
- 愤怒的小鸟【$DP$优化】
卡常的状压\(DP\),愤怒的小鸟. 其实本来是个很水的状压\(DP\),但因为最后三个点\(n=18\),成功地把我的不可能达到的下界为\(\Omega(2^nn^2)\),紧确的上界为\(O(2^ ...