CF513G1.Inversions problem
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation of n numbers _p_1, _p_2, ..., p__n. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p__l, p__l + 1, ..., p__r. Your task is to find the expected value of the number of inversions in the resulting permutation.
给你一个 $ n $ 个数的排列 $ p_1,,p_2,,\dots,,p_n $。我们执行 $ k $ 次如下操作:均匀随机地选择两个下标 $ l $ 和 $ r $(满足 $ l \leq r $),并将子数组 $ p_l,,p_{l+1},,\dots,,p_r $ 中的元素顺序反转。你的任务是求最终排列中逆序对数量的期望值。
输入格式
The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 109). The next line contains n integers _p_1, _p_2, ..., p__n — the given permutation. All p__i are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
- In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold.
- In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold.
- In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 109 will hold.
输入的第一行包含两个整数 n 和 k(1 ≤ n ≤ 100,1 ≤ k ≤ 109)。第二行包含 n 个整数 p1,p2,...,pn —— 给定的排列。所有 pi 互不相同,且取值范围为 1 到 n。
本题包含三个子问题。各子问题对输入的约束条件不同。正确提交每个子问题可获得相应分数。各子问题的描述如下:
- 子问题 G1(3 分):约束条件为 1 ≤ n ≤ 6,1 ≤ k ≤ 4。
- 子问题 G2(5 分):约束条件为 1 ≤ n ≤ 30,1 ≤ k ≤ 200。
- 子问题 G3(16 分):约束条件为 1 ≤ n ≤ 100,1 ≤ k ≤ 109。
输出格式
Output the answer with absolute or relative error no more than 1_e_ - 9.
以绝对或相对误差不超过 10−9 输出答案。
输入输出样例
输入#1
3 1 1 2 3
输出#1
0.833333333333333
输入#2
3 4 1 3 2
输出#2
1.458333333333334
说明/提示
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability
, the interval will consist of a single element and the permutation will not be altered. With probability
we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability
the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to
.
考虑第一个样例测试。我们将随机选取排列 (1,2,3)(该排列不含逆序对)的一个区间,并将该区间内元素的顺序反转。以概率
,所选区间仅包含一个元素,因此排列保持不变。以概率
,我们将前两个元素的顺序反转,得到排列 (2,1,3),该排列含有 1 个逆序对。同样以该概率,我们可能选取由后两个元素构成的区间,从而得到排列 (1,3,2),该排列也含有 1 个逆序对。最后,以概率
,随机选取的区间将包含全部三个元素,从而得到排列 (3,2,1),该排列含有 3 个逆序对。因此,逆序对数的期望值为
。
输入解题思路,AI测评打分。不知道怎么写?