Inversions After Shuffle CodeForces - 749E (概率,期望)
大意: 给定一个$n$排列, 随机选一个区间, 求将区间随机重排后整个序列的逆序对期望.
考虑对区间$[l,r]$重排后逆序对的变化, 显然只有区间[l,r]内部会发生改变
而长为$k$的随机排列期望逆序为$\frac{k(k-1)}{4}$(证明考虑逆序与顺序对称性)
所以$[l,r]$的贡献即为$inv(1,n)-inv(l,r)+\frac{(r-l+1)(r-l)}{4}$
所以就转化为求$\sum\limits_{1\le l\le r\le n}inv(l,r)$
对于逆序对$(x,y)$, 我们枚举$y$, 就有贡献$(n-y+1)\sum\limits_{\substack{1\le x< y\\ a_y<a_x}}x$
就转化为二维数点问题, 可以用树状数组解决.
用$long\space double$不知道为什么会WA, 改成$double$直接过了
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef double db; const int N = 1e5+10;
int n;
db c[2][N];
void add(int id, int x, int v) {
for (; x; x^=x&-x) c[id][x]+=v;
}
db qry(int id, int x) {
db ret = 0;
for (; x<=n; x+=x&-x) ret+=c[id][x];
return ret;
}
int main() {
scanf("%d", &n);
db ans = 0;
REP(i,1,n) {
int t;
scanf("%d", &t);
ans += qry(0,t)*n*(n+1)/2-(n-i+1)*qry(1,t)+((db)i*i*i-i)/12;
add(0,t,1), add(1,t,i);
}
ans /= (db)n*(n+1)/2;
printf("%.12lf\n", ans);
}
Inversions After Shuffle CodeForces - 749E (概率,期望)的更多相关文章
- 【codeforces 749E】 Inversions After Shuffle
http://codeforces.com/problemset/problem/749/E (题目链接) 题意 给出一个1~n的排列,从中等概率的选取一个连续段,设其长度为l.对连续段重新进行等概率 ...
- Codeforces - 1264C - Beautiful Mirrors with queries - 概率期望dp
一道挺难的概率期望dp,花了很长时间才学会div2的E怎么做,但这道题是另一种设法. https://codeforces.com/contest/1264/problem/C 要设为 \(dp_i\ ...
- 【BZOJ-1419】Red is good 概率期望DP
1419: Red is good Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 660 Solved: 257[Submit][Status][Di ...
- uvalive 7331 Hovering Hornet 半平面交+概率期望
题意:一个骰子在一个人正方形内,蜜蜂在任意一个位置可以出现,问看到点数的期望. 思路:半平面交+概率期望 #include<cstdio> #include<cstring> ...
- Inversions After Shuffle
Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...
- OI队内测试一【数论概率期望】
版权声明:未经本人允许,擅自转载,一旦发现将严肃处理,情节严重者,将追究法律责任! 序:代码部分待更[因为在家写博客,代码保存在机房] 测试分数:110 本应分数:160 改完分数:200 T1: 题 ...
- 2016 多校联赛7 Balls and Boxes(概率期望)
Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...
- 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】
链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...
- 【bzoj4832】[Lydsy2017年4月月赛]抵制克苏恩 概率期望dp
题目描述 你分别有a.b.c个血量为1.2.3的奴隶主,假设英雄血量无限,问:如果对面下出一个K点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输入 输入包含多局游戏. 第一行包含一个整数 T (T ...
随机推荐
- 数据预测算法-ARIMA预测
简介 ARIMA: AutoRegressive Integrated Moving Average ARIMA是两个算法的结合:AR和MA.其公式如下: 是白噪声,均值为0, C是常数. ARIMA ...
- 新建Spring boot 启动报错 Failed to auto-configure a DataSource
今天学习springboot,使用idea创建项目.在选择组件时添加了mysq 然后在第一次启动的时候启动报错,错误信息如下: ***************************APPLICATI ...
- Filter、Listener、Interceptor、Controller in a Request
从以下程序运行Log 可以看出在一个Request 执行过程中 MyListener>>requestInitialized >>> MyFilter>> ...
- mybatis plus 报错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 少了个范型
- 界面之下:还原真实的 MV* 模式
界面之下:还原真实的MV*模式 作者:戴嘉华 转载请注明出处并保留原文链接( https://github.com/livoras/blog/issues/11 )和作者信息. 目录: 前言 MVC ...
- js函数收集
常见js函数收集: 转自:http://www.qdfuns.com/notes/36030/2eb2d45cccd4e62020b0a6f0586390af.html //运动框架 function ...
- 关于Layui的表格中分页处理
table.render({ elem: '#test' ,height:'full-125' ,url:'data.php' ,cellMinWidth: 80 //全局定义常规单元格的最小宽度,l ...
- git使用遇到的问题
1.我新建了一个django项目,然后又在git上新建了一个仓库,然后我在django的项目文件内,将git上的项目clone到这个文件内的时候 git clone https://gitee.com ...
- 慕课网_Java入门第三季
第1章 异常与异常处理 1-1 Java异常简介 (06:50) 1-2 Java中使用try..catch..finally实现异常处理 (05:08) import java.util.Input ...
- 【C++学习笔记】static 关键字
(阅读<C++ primer plus>可知 C++的static关键字跟Java还是很类似的) 为什么需要static关键字:在文件A中定义的非局部变量language,在文件B中可以通 ...