CodeForces - 258D Little Elephant and Broken Sorting

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 11 21 2

Output

0.500000000

Input

4 31 3 2 41 22 31 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;
}

  

 

原文地址:https://www.cnblogs.com/JYYHH/p/8976706.html

时间: 2024-10-27 01:37:17

CodeForces - 258D Little Elephant and Broken Sorting的相关文章

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)\) -

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

题面 \(solution:\) 这道题主要难在考场上能否想到这个思路(即如何设置状态)(像我这样的蒟蒻就想不到呀QAQ)不过这一题确实很神奇! \(f[i][j]:\)表示第 \(a_i\) 个数比第 \(a_j\) 个数大的几率,这样设置状态比较好转移:对于每一次 \(a_i\) 与 \(a_j\) 的交换,他只会影响到序列里,每一个数与\(a_i\),\(a_j\) 的胜率(一共有\(n\)次交换,只要每次交换复杂度在\(O(n)\)级别这道题就解决了了).而且我们不难发现转移时每一个数与

CF258D. Little Elephant and Broken Sorting

传送门 题意: 有一个1~n的排列,依次进行m次操作,第i次操作表示为\((x _i,y_i)\),交换以这两个值为下标的元素,每次操作有一半的概率成功,你需要求出最后序列的逆序对的期望个数. 分析: 因为逆序对是(x,y)的形式,那么考虑每一对(i,j)对答案的贡献. \(f_{i,j}\)表示\(a_i>a_j\)的概率,我们发现每一个操作只影响\(O(n)\)个f值,于是可以处理出每一个f的初始值,每次更新受影响的值即可. 时间复杂度 \(O(NM)\), 空间复杂度 \(O(N^2)\)

CodeForces - 204C Little Elephant and Furik and Rubik

CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚举所有字串,再枚举所有位置,算出所有答案不就行了 我们自然不能无脑暴力,但是暴力可以给我们启发 我们知道所有对答案做出贡献的字符一定是相同的(废话) 所以我们可以O(n^2)首先枚举两个字符串中相同的字符然后再考虑如何贡献 然后计算出所有的方案下的值,再除以n*(n+1)*(2*n+1)/6 [不知

Codeforces 220B - Little Elephant and Array 离线树状数组

This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will solve this problem in offline. For each x (0?≤?x?<?n) we should keep all the queries that end in x. Iterate that x from 0 to n?-?1. Also we need to kee

CodeForces 258B Little Elephant and Elections 数位DP

前面先用数位DP预处理,然后暴力计算组合方式即可. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include

CodeForces 221D Little Elephant and Array

Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 221D64-bit integer IO format: %I64d      Java class name: (Any) The Little Elephant loves playing with arrays. He has array a,

CodeForces 259A Little Elephant and Chess

Little Elephant and Chess Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 259A Description The Little Elephant loves chess very much. One day the Little Elephant and his friend decided t

Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS

C. Sorting Railway Cars An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increa