题目大意

给定一个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. java mysql多次事务 模拟依据汇率转账,并存储转账信息 分层完成 dao层 service 层 client层 连接池使用C3p0 写入库使用DBUtils

    Jar包使用,及层的划分 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3 ...

  2. ES6初识-解构赋值

    数组解构赋值 [a,b]=[1,2]; . 方法返回 function f(){ return [1,2] } let a,b; [a,b]=f();//a=1,b=2   function f1() ...

  3. 深入浅出:了解JavaScript的ES6、ES7新特性

    参照阮一峰博客:http://es6.ruanyifeng.com/#README es6常见题:https://blog.csdn.net/qq_39207948/article/details/8 ...

  4. Python全栈day 05

    Python全栈day 05 一.数据类型补充 1. int py2和py3的2种区别 py2有int和long,int的取值范围为-2^31~2^31-1,超出范围自动转为long,长整型. py2 ...

  5. qt5.10.1编译记录

    qt版本更新比较快,不知道选哪个版本合适,故选择一个较新版本的. 平台imx6    +    linux4.1.16   +   qt5.10.1 采用明远智睿提供的编译器:fsl-imx-fb-g ...

  6. Android 时间计算工具 通用类TimeUtil

    1.整体分析 1.1.源代码如下,可以直接Copy. public class TimeUtil { private static final String TAG = "TimeUtil& ...

  7. linux环境下kettle部署(JDK安装配置,kettle安装配置,资源库配置,定时执行job)

    一.部署准备 1.1 java安装(略) 1.2 JDK配置 1.     命令行键入“cd /etc”进入etc目录 2.     命令行键入“vi profile”打开profile文件 3.   ...

  8. laravel5.5容器

    目录 1. 比较典型的例子就是 cache 缓存 2. 容器顾名思义,其实就是完成存取过程 2.1 绑定过程 简单绑定 绑定单例 绑定实例 绑定初始数据 2.2 解析过程 容器主要是为了实现控制反转, ...

  9. 通过学习制作长微博工具来了解水印的制作,及EditText中的内容在图片中换行显示

    长微博工具非常有用,140字的要求可能阻止你写更多的内容,于是长微博工具应运而生,虽然网上有很多长微博工具,但是我都不是很满意,所以自己想做一个,通过做这个长微博工具,我学习到了很多东西,有两个难点, ...

  10. USACO Section1.1 Friday the Thirteenth 解题报告

    friday解题报告 —— icedream61 博客园(转载请注明出处) -------------------------------------------------------------- ...