题目传送门:CF749E

记一道傻逼计数题。

题意简述:

给一个 \(1\) 到 \(n\) 的排列,随机选取区间 \([l,r]\) 随机打乱区间内的元素,问打乱后的整个序列的逆序数期望。

题解:

下面是代码,复杂度 \(\mathcal{O}(n\log n)\)。

#include <cstdio>
#include <cstring> typedef long long LL;
typedef double db;
const int MN = 100005; int N, A[MN];
LL pre[MN], suf[MN], Inv;
LL b0[MN], b1[MN], b2[MN];
inline void add(LL *b, int i, LL x) { for (; i <= N; i += i & -i) b[i] += x; }
inline LL qur(LL *b, int i) { LL a = 0; for (; i; i -= i & -i) a += b[i]; return a; }
db Ans; int main() {
scanf("%d", &N), Ans = (db)(N - 1) * (N + 2) / 24;
for (int i = 1; i <= N; ++i) scanf("%d", &A[i]);
for (int i = 1; i <= N; ++i) add(b0, A[i], 1), pre[i] = pre[i - 1] + i - qur(b0, A[i]);
Inv = pre[N], memset(b0, 0, sizeof b0);
for (int i = N; i >= 1; --i) suf[i] = suf[i + 1] + qur(b0, A[i]), add(b0, A[i], 1);
Ans += 2 * Inv;
for (int i = 1; i <= N; ++i) Ans -= 2. * (i * pre[i] + (N - i + 1) * suf[i]) / N / (N + 1);
memset(b0, 0, sizeof b0);
for (int i = N; i >= 1; --i) {
Ans -= (db)(qur(b2, A[i]) * i * i + qur(b1, A[i]) * i + qur(b0, A[i])) / N / (N + 1);
add(b0, A[i], (LL)i * (i - 1));
add(b1, A[i], 1 - 2 * i);
add(b2, A[i], 1);
}
printf("%.15lf\n", Ans);
return 0;
}

Codeforces 749E: Inversions After Shuffle的更多相关文章

  1. 【codeforces 749E】 Inversions After Shuffle

    http://codeforces.com/problemset/problem/749/E (题目链接) 题意 给出一个1~n的排列,从中等概率的选取一个连续段,设其长度为l.对连续段重新进行等概率 ...

  2. Inversions After Shuffle

    Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. Inversions After Shuffle CodeForces - 749E (概率,期望)

    大意: 给定一个$n$排列, 随机选一个区间, 求将区间随机重排后整个序列的逆序对期望. 考虑对区间$[l,r]$重排后逆序对的变化, 显然只有区间[l,r]内部会发生改变 而长为$k$的随机排列期望 ...

  4. Codeforces 749E Gosha is hunting 二分+DP

    很神奇的一题 看完题解不由惊叹 题意:$n$个神奇宝贝 $a$个普通球 $b$个高级球 普通球抓住$i$神奇宝贝的概率为$u[i]$ 高级球为$p[i]$ 一起用为$u[i]+p[i]-u[i]*p[ ...

  5. Codeforces Round #388 (Div. 2)

      # Name     A Bachgold Problem standard input/output 1 s, 256 MB    x6036 B Parallelogram is Back s ...

  6. Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing

    链接: https://codeforces.com/contest/1278/problem/A 题意: Polycarp has built his own web service. Being ...

  7. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  8. Codeforces 513G1 513G2 Inversions problem [概率dp]

    转自九野:http://blog.csdn.net/qq574857122/article/details/43643135 题目链接:点击打开链接 题意: 给定n ,k 下面n个数表示有一个n的排列 ...

  9. codeforces 301 E. Infinite Inversions

    题目:   time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

随机推荐

  1. 【CFGym102059G】Fascination Street(思维DP)

    点此看题面 大致题意: 有\(n\)个路灯,每个路灯有一定的建造费用,且建成后可照亮自身及周围距离为\(1\)的两个格子.你可以交换\(k\)次两个路灯的建造费用,求照亮所有格子的最小费用. 题意转换 ...

  2. Educational Codeforces Round 70 题解

    噩梦场. 题目出奇的难,好像一群外国老哥看 A 看着看着就哭了-- A 找到 \(b\) 最低的 \(1\),这个 \(1\) 肯定要跟 A 中的一个 \(1\) 搭配,而且是能搭配的 \(1\) 中 ...

  3. [LeetCode] 839. Similar String Groups 相似字符串组

    Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it ...

  4. 认识map-reduce

    基本概念 map-reduce1.0 例子: hadoop streaming 用语言驱动map-reduce的话,使用的hadoop streaming命令,可以通过python,php,java来 ...

  5. 洛谷疯狂coding~

    1.关于数学建模思想在coding之中的应用. 将马路作为一条数轴,每棵树的位置作为数轴上的坐标点,再将坐标点与数组的下标联系到一起,完成建模. 2.本题坑点在于对“其中有多少个数,恰好等于集合中另外 ...

  6. SQL -------- 简单的增删改查

    sql  结构化查询语言,一种ansi 的标准计算机语言,为了访问数据库 可以做什么:可以对数据库 和表进行创建于删除, 对表里面的数据进行增删改查. 也可以创建存储过程和视图,对表设置权限 RDBM ...

  7. java中 Math和StrictMath

    今天无意中看到java api中有StrictMath 这个工具类,发现它部分调用实现是用了Math中的实现.Math 这个类API 1.0版本就有了,StrictMath API是1.3版本才出来的 ...

  8. Jenkins整合SonarQube

    一.概述 安装SonarQube,参考链接: https://www.cnblogs.com/xiao987334176/p/12011623.html 安装SonarQube Scanner,参考链 ...

  9. 如何将云上的Linux文件自动备份到本地服务器

    需求场景: 将云上一台Linux服务器文件备份到本地服务器,一周一备即可. 面对这样一个需求,我们可能面临下列几个问题, 备份方式:是云服务器推文件到本地服务器写入,还是本地服务器从云服务器拉文件?这 ...

  10. Mysql系列(十一)—— 性能分析慢查询日志

    转载自:http://www.cnblogs.com/kerrycode/p/5593204.html 慢查询日志概念 MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响 ...