题目传送门: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. [LeetCode] 598. Range Addition II 范围相加之二

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  2. Via板载声卡底噪严重、播放卡顿及耳机与扬声器音源切换问题【解决方法】

    HD VDeck[VIA威盛HD audio系列音频驱动] 关闭音效增强之后,一切正常............ 默默骂一句VIA沙雕 另外附上:开启耳机和扬声器独立音源的设置 注册表 Computer ...

  3. java 声明并初始化整型变量

    public class Sample { public static void main(String[] args) { int num; num = ; System.out.println(& ...

  4. 分页条件传参bug之解决

    问题描述:以对象作为参数,对象中包含PageNum.PageSize.Condition对象等.对应的@RequestBody为如PageReqDTO reqDTO时,如果使用postman时,不在b ...

  5. js原型和原型链,以及__proto__、prototype属性

    __proto__和prototype属性: 1.__proto__属性: 在JS里,万物皆对象(函数是对象.原型也是对象...).对象都具有属性__proto__,这个属性会指向该对象的原型. 2. ...

  6. vscode java

    https://devblogs.microsoft.com/visualstudio/announcing-visual-studio-code-java-installer/ public cla ...

  7. SpringBoot第十八篇:异步任务

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11095891.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   系统中的异 ...

  8. select2的简单使用

    静态下拉列表 修改 type_template.html  引入JS <!-- slect2插件--> <link rel="stylesheet" href=& ...

  9. 基于word2vec的文档向量模型的应用

    基于word2vec的文档向量模型的应用 word2vec的原理以及训练过程具体细节就不介绍了,推荐两篇文档:<word2vec parameter learning explained> ...

  10. CMake方式编译

    [1]CMake基础 CMake是一种跨平台编译工具 CMake主要是编写CMakeLists.txt文件 通过CMake命令将CMakeLists.txt文件转化为make所需的Makefile文件 ...