CodeForces 258D Little Elephant and Broken Sorting(期望)
CF258D Little Elephant and Broken Sorting
题意
题意翻译
有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\)。每次操作都有\(50\%\)的概率进行。
求进行\(m\)次操作以后的期望逆序对个数。
\(n,m\le 1000\)
输入输出格式
输入格式:
The first line contains two integers \(n\) and \(m\) \((1\leq n,m\leq 1000,n>1)\) — the permutation size and the number of moves. The second line contains \(n\) distinct integers, not exceeding \(n\) — the initial permutation. Next \(m\) lines each contain two integers: the \(i\)-th line contains integers \(a_{i}\) and \(b_{i}\) \((1\leq a_{i},b_{i}\leq n,a_{i}\neq b_{i})\) — the positions of elements that were changed during the \(i\)-th move.
输出格式:
In the only line print a single real number — the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed \(10^{-6}\).
输入输出样例
输入样例#1:
2 1
1 2
1 2
输出样例#1:
0.500000000
输入样例#2:
4 3
1 3 2 4
1 2
2 3
1 4
输出样例#2:
3.000000000
思路
这道题真的水。 --Mercury
完全想不到的状态设计,感谢\(Mercury\)巨佬的指点。
定义\(f(i,j)\)为位置\(i\)上的数比位置\(j\)上的数大的概率。假设每次交换都是\(100\%\)成功的,不妨设这次交换的数的下标为\(a,b\),那么对于任意的\(f(i,a),f(i,b)\)就要被交换,\(f(a,i),f(b,i)\)也要被交换。可是当前交换的概率是\(50\%\)的,所以\(f(i,a),f(i,b)\)之间的差值要被分别减少\(50\%\),也就相当于\(f(i,a)=f(i,b)=(f(i,a)+f(i,b))\div 2\)。同理,\(f(a,i)=f(b,i)=(f(a,i)+f(b,i))\div 2\)。最后的逆序对期望,也就是\(\Sigma [i<j]f(i,j)\times 1\),也就是\(\Sigma [i<j]f(i,j)\)。
还要再胡扯两句。 其实只要想出了\(f(i,j)\)这个东西,什么都简单了,可是又会有几个人能够想到这种方法呢?完全没有类似的情况作为参考,掌握了这道题却又能给类似的题提供经验(毕竟也没有类似的题)。下一次见到了这种思维量大的题,还是不太能想得出。思维的活跃在\(OI\)中还是有很大的作用的啊!
AC代码
#include<bits/stdc++.h>
#define RG register
using namespace std;
int n,m,a[1005];
double ans,f[1005][1005];
int read()
{
RG int re=0;RG char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) re=(re<<3)+(re<<1)+ch-'0',ch=getchar();
return re;
}
int main()
{
n=read(),m=read();
for(RG int i=1;i<=n;i++) a[i]=read();
for(RG int i=1;i<=n;i++)
for(RG int j=i+1;j<=n;j++)
if(a[i]>a[j]) f[i][j]=1.0;
else f[j][i]=1.0;
while(m--)
{
RG int x=read(),y=read();
if(x==y) continue;
for(RG int i=1;i<=n;i++)
{
if(i==x||i==y) continue;
f[i][x]=f[i][y]=(f[i][x]+f[i][y])/2;
f[x][i]=f[y][i]=(f[x][i]+f[y][i])/2;
}
f[x][y]=f[y][x]=0.5;
}
for(RG int i=1;i<=n;i++)
for(RG int j=i+1;j<=n;j++)
ans+=f[i][j];
printf("%.8f",ans);
return 0;
}
CodeForces 258D 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
Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of al ...
- CF 258 D. Little Elephant and Broken Sorting
D. Little Elephant and Broken Sorting 链接 题意: 长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$\frac{1}{2}$,求m此操作后逆序对的期 ...
- CodeForces - 258D:Little Elephant and Broken Sorting(概率DP)
题意:长度为n的排列,m次交换xi, yi,每个交换x,y有50%的概率不发生,问逆序数的期望 .n, m <= 1000 思路:我们只用维护大小关系,dp[i][j]表示位置i的数比位置j的 ...
- 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
D. Little Elephant and Broken Sorting time limit per test 2 seconds memory limit per test 256 megaby ...
- codeforces 258D DP
D. Little Elephant and Broken Sorting time limit per test 2 seconds memory limit per test 256 megaby ...
- CodeForces - 204C Little Elephant and Furik and Rubik
CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚 ...
随机推荐
- hdu多校第六场1012 (hdu6645) Stay Real 假博弈,真贪心
题意: 给你一个小根堆,从根开始拿,拿走子节点被拿完后才可以拿走父节点,两个人依次拿,谁拿的节点总和大谁获胜,问你谁有必胜策略. 题解: 小根堆中,每个点的权值总是不小于父亲节点的权值.所以无论怎么取 ...
- python爬取文件时,内容为空
解决方式: img_res = requests.get(src,headers=header)在header中加上referer防盗链加上防盗链header的例子: header = {" ...
- HDU 5726 线段树+dp
题意:给出一个序列,后q次询问,求给定区间gcd及整个序列有多少个序列的gcd和这个值相同 首先线段树维护区间gcd建树,之后预处理出每个gcd有多少个子序列,这时需要dp, dp[i][tmp]表示 ...
- 随笔-ansible-4
触发器: 一个任务同时调用多个触发器: 为远程主机上的用户设置环境变量: 保存前一步命令的输出结果,并保存到foo中: 添加环境变量的另一种方式: 注意:lineinfile模块只适用于修改少量环境变 ...
- vue-router配置子路由
1.改写App.vue 里面的代码 ,增加路由跳转,增加Hi页面1,Hi页面2的跳转 2.修改HI.vue 里面的内容,增加 <router-view class="aaa" ...
- Fence Obstacle Course
Fence Obstacle Course 有n个区间自下而上有顺序的排列,标号\(1\sim n\),第i个区间记做\([l_i,r_i]\),现在从第n个区间的起点s出发(显然s在\([l_n,r ...
- 微信小程序为什么看不到所有的console.log()的日志信息
记录一个巨傻无比的问题 1.在首页的onLoad()函数里面,加了地理位置的加载,并打印到控制台上,可是今天就是没出现 2.然后纳闷的很久,各种google,发现没有人遇到这个问题 3.再然后,我就看 ...
- BIO、NIO、AIO入门认识
同步.异步.阻塞.非阻塞概念理解. 同步: 比如在执行某个逻辑业务,在没有得到结果之前一直处于等待阻塞状态,得到结果后才继续执行 异步: 比如在执行某个逻辑业务,在没有得到结果可以去干其他的事情,等待 ...
- thinkphp 获取内容
如果需要获取渲染模板的输出内容而不是直接输出,可以使用fetch方法. fetch方法的用法和display基本一致(只是不需要指定输出编码和输出类型): 大理石平台规格 fetch('模板文件') ...
- php设置时区和strtotime转化为时间戳函数
date_default_timezone_set('PRC');//设置中华人民共和国标准时间 strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳 格式:int strto ...