codeforces 258D
2 seconds
256 megabytes
standard input
standard output
The Little Elephant loves permutations of integers from 1 to n very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1, 2, 3, ..., n.
This time the Little Elephant has permutation p1, p2, ..., pn. Its sorting program needs to make exactly m moves, during the i-th move it swaps elements that are at that moment located at the ai-th and the bi-th positions. But the Little Elephant's sorting program happened to break down and now on every step it can equiprobably either do nothing or swap the required elements.
Now the Little Elephant doesn't even hope that the program will sort the permutation, but he still wonders: if he runs the program and gets some permutation, how much will the result of sorting resemble the sorted one? For that help the Little Elephant find the mathematical expectation of the number of permutation inversions after all moves of the program are completed.
We'll call a pair of integers i, j (1 ≤ i < j ≤ n) an inversion in permutatuon p1, p2, ..., pn, if the following inequality holds: pi > pj.
The first line contains two integers n and m (1 ≤ n, m ≤ 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 ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — 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.
2 1
1 2
1 2
0.500000000
4 3
1 3 2 4
1 2
2 3
1 4
3.000000000 思路:对每一对位置i,j计算 f[i][j](p[i]>=p[j]的概率),当交换a,b位置时 对所有i有:f[i][a]=f[i][b]=(f[i][a]+f[i][b])/2,f[a][i]=f[b][i]=(f[a][i]+f[b][i])/2;
代码:
#include<bits/stdc++.h>
//#include<regex>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define inf 0x3f3f3f3f3f3f3f3f
#define fr(i, a, b) for(int i=a;i<=b;i++)
const int N = 1e3 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
int p[N];
db f[N][N];
int main()
{
int n,m;
ci(n),ci(m);
for(int i=;i<=n;i++) ci(p[i]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
f[i][j]=(p[i]>p[j]);//初始状态
while(m--)
{
int a,b;
ci(a),ci(b);
for(int i=;i<=n;i++){//更新
if(i!=a&&i!=b){
f[i][a]=f[i][b]=(f[i][a]+f[i][b])/;
f[a][i]=f[b][i]=(f[a][i]+f[b][i])/;
}
}
f[a][b]=f[b][a]=0.5;
}
db ans=;
for(int i=;i<=n;i++){//逆序数对和
for(int j=i+;j<=n;j++){
ans+=f[i][j];
}
}
pd(ans);
return ;
}
codeforces 258D的更多相关文章
- 【Codeforces 258D】 Count Good Substrings
[题目链接] http://codeforces.com/contest/451/problem/D [算法] 合并后的字符串一定是形如"ababa","babab&qu ...
- 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(概率DP)
题意:长度为n的排列,m次交换xi, yi,每个交换x,y有50%的概率不发生,问逆序数的期望 .n, m <= 1000 思路:我们只用维护大小关系,dp[i][j]表示位置i的数比位置j的 ...
- codeforces 258D DP
D. Little Elephant and Broken Sorting time limit per test 2 seconds memory limit per test 256 megaby ...
- 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 ...
- CodeForces 258D Little Elephant and Broken Sorting(期望)
CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- 【Beta】Daily Scrum Meeting——Day2
站立式会议照片 1.本次会议为第一次Meeting会议: 2.本次会议在中午12:00,在图书馆一楼楼道召开,本次会议为30分钟讨论今天要完成的任务以及接下来的任务安排. 燃尽图 每个人的工作分配 成 ...
- JAVA课程设计个人博客 学生成绩管理 201521145048 林健
1. 团队课程设计博客链接 http://www.cnblogs.com/kawajiang/p/7062407.html 2.个人负责模块或任务说明 本人主要负责支持用户登录.验证操作,显示设计界面 ...
- 201521123060 《Java程序设计》第9周学习总结
1.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2.书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1截图你的提交结果(出现学号) 1.2自己以前编写 ...
- 201521123096《Java程序设计》第十四周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...
- 201521123119《Java程序设计》第10周学习总结
1. 本周学习总结 Q1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 Q1.finally 题目4-2 Q1.1 截图你的提交结 ...
- 03标准对象-01-Date和JSON
0.写在前面的话 在JS世界中,一切都是对象,区别对象类型使用tyepof,返回一个字符串,如: typeof 123; // 'number' typeof NaN; // 'number' typ ...
- OJ周末题
字符串分割 字符统计 记票统计
- Extjs整合CKEditor富文本编辑器插件
CKEditor插件官方下载地址: http://ckeditor.com/download/releases 我使用的版本是 ExtJS5.1.0 CKEditor4.4.8 参考文章: http ...
- SQL映射文件实现多种方式查询
1.单条件查询在test中代码如下 2.多条件查询时需要把查询条件编辑为对象或者是集合传入,例如 通过对象进行查询 或者是通过集合进行查询列如Map集合 还有就是通过@Param注解实现多参数的入参, ...
- 自定义工作流活动报错:您无法登陆系统。原因可能是您的用户记录或您所属的业务部门在Microsoft Dynamics 365中已被禁用。
本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复265或者20170926可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me ...