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. VBA连接MySQL数据库以及ODBC的配置(ODBC版本和MySQL版本如果不匹配会出现驱动和应用程序的错误)

    db_connected = False '获取数据库连接设置dsn_name = Trim(Worksheets("加载策略").Cells(2, 5).Value)  ---- ...

  2. JVM——自定义类加载器

    )以上两种情况在实际中的综合运用:比如你的应用需要通过网络来传输 Java 类的字节码,为了安全性,这些字节码经过了加密处理.这个时候你就需要自定义类加载器来从某个网络地址上读取加密后的字节代码,接着 ...

  3. BZOJ 4393: [Usaco2015 Dec]Fruit Feast

    DP #include<cstdio> using namespace std; int T,A,B,F[5000005],G[5000005]; int main(){ scanf(&q ...

  4. Eclipse配置Maven工具

    1.Maven安装,下载Maven二进行制文件: http://maven.apache.org/download.cgi 下载后解压,然后设置maven的bin目录到系统环境变量Path中,在cmd ...

  5. 【Swap Nodes in Pairs】cpp

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...

  6. 【Python Selenium】简单数据生成脚本

    最近因工作需要,写了一个简单的自动化脚本,纯属学习,顺便学习下selenium模块. 废话不多说,直接上代码!! 这里一位大神重写了元素定位.send_keys等方法,咱们直接进行调用. 适用Pyth ...

  7. SQL 基础语法详解

    SQL 命令一般分为 DQL.DML.DDL DQL:数据查询语句,基本就是 SELECT 查询命令,用于数据查询 DML:Data Manipulation Language 的简称,即数据操纵语言 ...

  8. android实现前置后置摄像头相互切换

    首先自定义一个继承自SurfaceView并且实现了SurfaceHolder.Callback接口的组件: public class CameraView extends SurfaceView i ...

  9. sql执行效率 Explain

    explain+sql语句 explain返回的结果项很多,这里我们只关注三种,分别是type,key,rows. key:显示MySQL实际决定使用的键(索引).如果没有选择索引,键是NULL. r ...

  10. socket实例C语言:一个简单的聊天程序

    我们老师让写一个简单的聊天软件,并且实现不同机子之间的通信,我用的是SOCKET编程.不废话多说了,先附上代码: 服务器端server.c #include <stdio.h> #incl ...