Discription

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 permutatuonp1, 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: thei-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

     设f[i][j] 为 a[i] 比 a[j] 大的概率,显然初始的时候 f[i][j] = [a[i] > a[j]],并且最后答案就等于Σf[i][j] (i<j)。
问题是怎么快速维护f[][]。
考虑一次只涉及两个位置,我们就暴力的修改一遍和这两个位置有关的数就好啦。
#include<bits/stdc++.h>
#define ll long long
#define D double
const int maxn=1005;
D f[maxn][maxn],ans;
int a[maxn],n,m,u,v;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",a+i);
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++) if(a[i]>a[j]) f[i][j]=1; else f[j][i]=1; while(m--){
scanf("%d%d",&u,&v);
for(int i=1;i<=n;i++) if(i!=u&&i!=v){
f[u][i]=f[v][i]=(f[u][i]+f[v][i])/2;
f[i][u]=f[i][v]=(f[i][u]+f[i][v])/2;
}
f[u][v]=f[v][u]=(f[u][v]+f[v][u])/2;
} for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++) ans+=f[i][j];
printf("%.11lf\n",ans);
return 0;
}

  

 

CodeForces - 258D 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. CF 258 D. Little Elephant and Broken Sorting

    D. Little Elephant and Broken Sorting 链接 题意: 长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$\frac{1}{2}$,求m此操作后逆序对的期 ...

  4. CodeForces - 258D:Little Elephant and Broken Sorting(概率DP)

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

  5. CF258D Little Elephant and Broken Sorting/AGC030D Inversion Sum 期望、DP

    传送门--Codeforces 传送门--Atcoder 考虑逆序对的产生条件,是存在两个数\(i,j\)满足\(i < j,a_i > a_j\) 故设\(dp_{i,j}\)表示\(a ...

  6. CF258D Little Elephant and Broken Sorting (带技巧的DP)

    题面 \(solution:\) 这道题主要难在考场上能否想到这个思路(即如何设置状态)(像我这样的蒟蒻就想不到呀QAQ)不过这一题确实很神奇! \(f[i][j]:\)表示第 \(a_i\) 个数比 ...

  7. codeforces 258D

    D. Little Elephant and Broken Sorting time limit per test 2 seconds memory limit per test 256 megaby ...

  8. codeforces 258D DP

    D. Little Elephant and Broken Sorting time limit per test 2 seconds memory limit per test 256 megaby ...

  9. CodeForces - 204C Little Elephant and Furik and Rubik

    CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚 ...

随机推荐

  1. TCP/IP网络编程之基于TCP的服务端/客户端(二)

    回声客户端问题 上一章TCP/IP网络编程之基于TCP的服务端/客户端(一)中,我们解释了回声客户端所存在的问题,那么单单是客户端的问题,服务端没有任何问题?是的,服务端没有问题,现在先让我们回顾下服 ...

  2. xgboost原理总结和代码展示

    关于xgboost的学习推荐两篇博客,每篇看2遍,我都能看懂,你肯定没问题 两篇方法互通,知识点互补!记录下来,方便以后查看 第一篇:作者:milter链接:https://www.jianshu.c ...

  3. vs code 快捷键总结

    返回上个光标:alt + ←列编辑模式:shift + alt + 鼠标左键

  4. python - 接口自动化测试 - RunTest - 测试用例加载执行/测试报告生成

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: run_test.py @ide: PyCharm Com ...

  5. [python][django学习篇][9]设计正在博客视图(3)

    需求: 真正的首页视图函数,当用户访问我们的博客首页时,他将看到我们发表的博客文章列表,就像 演示项目 里展示的这样.t https://docs.djangoproject.com/en/1.10/ ...

  6. 【转】Unity3D研究院之两种方式播放游戏视频

    http://www.xuanyusong.com/archives/1019   Unity3D中播放游戏视频的方式有两种,第一种是在游戏对象中播放,就好比在游戏世界中创建一个Plane面对象,摄像 ...

  7. PAT 甲级 1047 Student List for Course

    https://pintia.cn/problem-sets/994805342720868352/problems/994805433955368960 Zhejiang University ha ...

  8. 低水平选手的自我救赎 (1)CLRS Exercise 16.5-2

    题目大意 给定正整数 $n$ 和一个由 $m$ 个正整数构成的可重集合 $A$,满足 $\forall a\in A, a\le n$ 且 $m\le n$ . 定义 $N_t(A) = |\{a\i ...

  9. 3973: seq

    3973: seq 题目描述 小y 的男朋友送给小y 一个数列{ai}{ai},并且刁难小y 要她维护这个序列. 具体而言,小y 的男朋友要求小y 完成两个操作: 1. 修改数列中的一个数 2. 设p ...

  10. bzoj 4465 游戏中的学问(game)

    题目描述 输入 输出 样例输入 3 1 1000000009 样例输出 2 提示 solution 令f[i][j]表示i个人围成j个圈的方案数 啥意思呢 可以把一个人塞进前面的圈里(i-1种塞法) ...