D. Little Elephant and Broken Sorting
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
2 1
1 2
1 2
output
0.500000000
input
4 3
1 3 2 4
1 2
2 3
1 4
output
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 DP的更多相关文章

  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(概率DP)

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

  3. Two Melodies CodeForces - 813D (DP,技巧)

    https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...

  4. Consecutive Subsequence CodeForces - 977F(dp)

    Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...

  5. codeforces的dp专题

    1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...

  6. Codeforces 721C [dp][拓扑排序]

    /* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG), ...

  7. CodeForces 607C (DP) Hard problem

    题目:这里 题意:给定n个字符串,每个字符串可以进行一项操作,就是将这个字符串交换,就是该字符串的第一个和最后一个交换,第二个和倒数第二个交换,以此类推,当然可以选择对于 该字符串进行或不进行这项操作 ...

  8. Codeforces 611d [DP][字符串]

    /* 题意:给一个长度不超过5000的字符串,每个字符都是0到9的数字. 要求将整个字符串划分成严格递增的几个数字,并且不允许前导零. 思路: 1.很开心得发现,当我在前i个区间以后再加一个区间的时候 ...

  9. Codeforces 404D [DP]

    /* 我是一个习惯后悔,但是没办法忍受内疚感的二货== 这题是个无脑dp,但是比赛大概20min没出...其实最后5min我好好想想简单化边界条件,可以出的. 题意: 给你一个长度为1e6的由?*01 ...

随机推荐

  1. Composition or inheritance for delegating page methods?

    引用链接:http://watirmelon.com/2011/01/24/composition-or-inheritance-for-delegating-page-methods/ Compos ...

  2. C 碎片七 指针

    一.地址和指针 程序在编译过程中,系统会根据变量类型分配一定长度的内存单元.内存区中的每个字节都有一个编号,该内存单元的初始编号就是变量的"地址/指针",该内存单元的长度就是变量的 ...

  3. agc016C - +/- Rectangle(构造 智商题)

    题意 题目链接 Sol 我的思路:直接按样例一的方法构造,若$h \times w$完全被$N \times M$包含显然无解 emm,wa了一发之后发现有反例:1 4 1 3 我的会输出[1 1 - ...

  4. Browser History

    History 对象中包含用户(在浏览器窗口中)访问过的URL History 对象是window对象的一部分,可通过window.history属性对其进行访问. 注释:没有应用于History对象 ...

  5. Ubuntu 16.04 远程登入root 用户

    安装 open ssh: sudo apt-get install openssh-server   修改 root 密码 sudo passwd root   以其他账户登录,通过 sudo nan ...

  6. Echarts图表学习

    最近项目已经运行的比较稳定了,正巧看到ECcharts的图标很炫,遂做一个玩玩,以备后面做数据分析使用. 官网地址:http://echarts.baidu.com/index.html 大致了解了下 ...

  7. c++输入

    1. char c = getchar(); 输入单个字符,可输入空格.换行符. 2. cin >> s; 不读取空格或换行符. 3. getline(cin, s); 输入一行到字符串s ...

  8. pat甲级1020中序后序求层序

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  9. 汶川大地震中的SAP成都研究院

    5·12汶川地震,发生于北京时间(UTC+8)2008年5月12日(星期一)14时28分04秒,此次地震的面波震级 里氏震级达8.0Ms.矩震级达8.3Mw,地震烈度达到11度.地震波及大半个中国及亚 ...

  10. POJ 3057 Evacuation(二分匹配)

    分析: 这是一个时间和门的二元组(t,d)和人p匹配的问题,当我们固定d0时,(t,d0)匹配的人数和t具有单调性. t增加看成是多增加了边就行了,所以bfs处理出p到每个d的最短时间,然后把(t,d ...