题目传送门: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] 554. Brick Wall 砖头墙壁

    There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The b ...

  2. VIPKID:笔试题(数组中和为0的一对数的数量,十进制转二进制中1的个数)

    1. 求数组中的和为0 的一对数的数量 注意,需要用到set import java.util.Scanner; public class Main{ public static void main( ...

  3. IDCode校验算法

    运行地址: https://c.runoob.com/compile/10 算法源码 public class HelloWorld { public static void main(String ...

  4. Nginx开启gzip提高页面加载速度

    今天发现页面多次请求数据且加载的数据大,需要等待的时间很长 记得Nginx有gzip,可以对数据进行压缩 gzip是nginx服务器的ngx_http_gzip_module模块提供的在线实时数据压缩 ...

  5. Composer 入门后,接下来该看看这篇文章了

    上篇文章,我们了解了 Composer 的包是如何开发的,过程中我们使用了 composer init. composer install. composerrequire. composer con ...

  6. 2018年Java面试题整理

    面试是我们每个人都要经历的事情,大部分人且不止一次,这里给大家总结最新的2018年面试题,让大家在找工作时候能够事半功倍.  1. Switch能否用string做参数? a. 在 Java 7 之前 ...

  7. 用友U9 查看功能页面实体

    对着当前页面右键查看属性:   在链接后面加上&__dm=true    在打开的页面将鼠标放到字段上,可以看到页面实体是那一个.

  8. 我是如何一步步编码完成万仓网ERP系统的(六)产品库设计 2.百度Ueditor编辑器

    https://www.cnblogs.com/smh188/p/11533668.html(我是如何一步步编码完成万仓网ERP系统的(一)系统架构) https://www.cnblogs.com/ ...

  9. 如何提升Web前端性能?

    什么是WEB前端呢?就是用户电脑的浏览器所做的一切事情.我们来看看用户访问网站,浏览器都做了哪些事情:输入网址 –> 解析域名 -> 请求页面 -> 解析页面并发送页面中的资源请求 ...

  10. nginx 反向代理时丢失端口的解决方案(转)

    今天,配置nginx反向代理时遇到一个问题,当设置nginx监听80端口时转发请求没有问题.但一旦设置为监听其他端口,就一直跳转不正常:如,访问欢迎页面时应该是重定向到登录页面,在这个重定向的过程中端 ...