题目大意

给定一个1到n的排列,然后随机选取一个区间,让这个区间内的数随机改变顺序,问这样的一次操作后,该排列的逆序数的期望是多少

首先,一个随机的长度为len的排列的逆序数是(len)*(len-1)/4,这是显然的,因为每种排列倒序一遍就会得到一个新序列,逆序数是len*(len-1)/2 - x(x为原排列的逆序数)

所以我们只需要把所有n*(n-1)/2的区间每种情况都随机化一遍再求逆序对,然后把这个值求和,就可以得到答案了

但是如果用朴素做法,那么复杂度是n^2的

考虑dp[x]表示以x为右端点的所有区间的逆序对数

dp[x] = sigma(dp[1~(x-1)]) + f[x]

f[x]表示x这个数对其以x为右端点所有区间的逆序对数所做的贡献,简单说,就是加了x以后,逆序对增加的个数

那么显然出现在第一位的数的贡献为1,而在第二位的数贡献为2,然后把相应的值加到权值线段树里

就可以统计出来所有的dp[x]了

最终答案就是 原序列的逆序对数-(Sum(dp[x])-Sum(所有区间随机化))/区间个数

PS:精度比较坑,中间整数运算尽量用long long,最后再用long double

#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
using namespace std;
typedef long double ld;
const int maxn = ;
long long tree[maxn*];
void Insert(int o, int l, int r, int k, int v)
{
if(l == r)
{
tree[o] = v;
return;
}
int mid = (l+r)>>;
if(k <= mid) Insert(o*, l, mid, k, v);
else Insert(o*+, mid+, r, k, v);
tree[o] = tree[o*] + tree[o*+];
} long long Query(int o, int l, int r, int L, int R)
{
if(L <= l && r <= R) return tree[o];
int mid = (l+r)>>;
ld ans = ;
if(L <= mid) ans += Query(o*, l, mid, L, R);
if(R > mid) ans += Query(o*+, mid+, r, L, R);
return ans;
}
int n, x, a[maxn], f[maxn];
int main()
{
cin>>n;
ld ans = ;
for(int i = ; i <= n; i++) cin>>a[i], f[a[i]] = i;
for(int i = ; i <= n; i++)
{
ans += Query(, , n, a[i], n);
Insert(, , n, a[i], );
}
ans *= ((long long)n*(n+));
memset(tree, , sizeof(tree));
long long last = ;
for(int i = ; i <= n; i++)
{
ans -= (last + Query(, , n, a[i], n));
last = last + Query(, , n, a[i], n);
Insert(, , n, a[i], f[a[i]]*);
}
for(int i = ; i <= n; i++)
ans += ((long long)(n-i+)*i*(i-)/);x`
ans /= ((long long)n*(n+));
cout<<setprecision()<<ans<<endl;
}

Codeforces Round #388 (Div. 2) 749E(巧妙的概率dp思想)的更多相关文章

  1. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  2. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  3. Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp

    B. Dreamoon and WiFi 题目连接: http://www.codeforces.com/contest/476/problem/B Description Dreamoon is s ...

  4. Codeforces Round #388 (Div. 2)

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

  5. Codeforces Round #388 (Div. 2) - C

    题目链接:http://codeforces.com/contest/749/problem/C 题意:给定一个长度为n的D/R序列,代表每个人的派别,然后进行发表意见,顺序是从1到n.每个人到他的回 ...

  6. Codeforces Round #388 (Div. 2) - B

    题目链接:http://codeforces.com/contest/749/problem/B 题意:给定平行四边形的3个点,输出所有可能的第四个点. 思路:枚举任意两个点形成的直线,然后利用这两个 ...

  7. Codeforces Round #388 (Div. 2) - A

    题目链接:http://codeforces.com/contest/749/problem/A 题意:给定一个数n,求把n分解成尽量多的素数相加.输入素数个数和具体方案. 思路:因为要尽量多的素数, ...

  8. Codeforces Round #388 (Div. 2) A+B+C!

    A. Bachgold Problem 任何一个数都可以由1和2组成,由于n是大于等于2的,也就是可以由2和3组成.要求最多的素数即素数越小越好,很明显2越多越好,如果n为奇数则再输出一个3即可. i ...

  9. Codeforces Round #388 (Div. 2) A,B,C,D

    A. Bachgold Problem time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. maven入门2

    1.修改maven本地仓库位置 没有效果,在新建项目时还是转跳到默认配置 在默认仓库位置添加修改后的setting文件,失败 修改成功,原因是前面修改的是setting而我们需要修改的是default ...

  2. Nagios 监控Windows服务器(详细篇)

    1. 监控内容 windows服务器的内部参数包括以下 a. 内存使用状况 b. CPU负载 c. 磁盘使用状况 d. 服务状态 e. 运行的进程 2. 监控原理 在windows服务器内安装NSCl ...

  3. 如何解决mysql中读取含表情符号的内容无法识别的问题

    当内容中包含有表情符号的时候,写入mysql时一般会设置字段或者表的charset为utf8mb4的形式: ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicod ...

  4. jquery横向手风琴效果2

    <!doctype html> <html> <head> <meta charset="utf-8"> <script ty ...

  5. Python面向对象--高级(一)

    ## 属性的类型 - 属性可分为类属性和实例属性 - 实例属性可以通过在类中使用self定义,或者直接在类外部使用实例变量定义 class Person(object): def __init__(s ...

  6. python版opencv:如何用笔记本摄像头拍照保存

    因为需要制作制作数据集 所以需要在笔记本上外置了一个logi的摄像头 准备使用python上得opencv来进行拍照 环境:opencv+pycharm+win10+py3 #coding:utf-8 ...

  7. centos安装xfce及输入法

    一.执行CentOS7 最小安装 去官网 https://www.centos.org/ 下载CentOS-7-x86_64-Minimal-1804.iso,然后使用rufus刻录U盘,安装之.安装 ...

  8. python-2函数

    http://docs.python.org/3/library/functions.html 或者菜鸟中文资料 1-使用函数 abs(-20)#求绝对值 max(1,4,200,3,2) #求最大的 ...

  9. java_servlet

    在servlet 解决中文乱码 response.setContentType("text/html;charset=utf-8"); //必须在getWrite()上面,不然依然 ...

  10. 笔记-docker-3 使用

    笔记-docker-3 使用 1.      镜像 image是docker最重要的概念,docker运行容器前需要本地存在对应的镜像,如果没有,会尝试从默认镜像库下载. 1.1.    镜像获取 查 ...