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 ...
随机推荐
- MDK Keil 5软件小技巧
几乎所有玩ARM Cortex M单片机的坛友都是通过MDK Keil 5或者IAR环境进行单片机的程序开发的,俗话说工欲善其事必先利其器,我们天天都在用这个开发环境,那么,有些在MDK Keil 5 ...
- spark 笔记 13: 再看DAGScheduler,stage状态更新流程
当某个task完成后,某个shuffle Stage X可能已完成,那么就可能会一些仅依赖Stage X的Stage现在可以执行了,所以要有响应task完成的状态更新流程. ============= ...
- shell脚本获取绝对路径
当前脚本全路径 echo $(readlink -f "$0") 获取绝对路径(不带文件名) echo $(dirname $(readlink -f "$0" ...
- docker top 和 docker exec ps 命令查看的PID区别
区别在于 docker top 查看到的 PID 属于宿主机的 PID.我们可以通过 宿主机执行 ps -ef 查看结果 也可以进去容器执行 top 和 ps查看结果
- OpenCV学习笔记(6)——几何变换
对图像进行各种变换,如移动,旋转,仿射变换等 变换 opencv提供了两个变换函数cv2.warpAffine cv2.warpPerspective使用这两个函数你可以实现所有类型的变换.前者接收的 ...
- CSS 解决 a标签去掉下划线 text-decoration: none无效 的解决方案
经过查阅,如果想要去掉a标签的默认效果,就要用text-decoration: none;,但是经过试验发现并不好用,可能是因为你用a标签里的class或id定义的CSS样式,就像这样: <di ...
- Computer Network Homework3’ s hard question
Computer Network Homework3’ s hard question 1. Which kind of protocol does CSMA belong to? A. Random ...
- ArcGIS客户端API中加载大量数据的几种解决办法
ArcGIS客户端API中加载大量数据的几种解决办法 2011-03-25 18:17 REST风格的一切事物方兴未艾,ArcGIS Server的客户端API(Javascript/Flex/Sil ...
- zabbix服务器性能监控工具的安装一
Linux下安装zabbix需要有LNMP运行环境,本篇记录安装LNMP的过程 目录 1.准备工作 2.安装mysql 3.安装nginx 4.安装php 1.准备工作 1.1准备一台centos6. ...
- 2 Configuring SAP ERP Sales and Distribution -introduction to SAP
First Steps in SAPWe’ll now discuss some of the basic menus, screens, and transactions that you need ...