Inversions After Shuffle CodeForces - 749E (概率,期望)
2024-09-05 14:02:01
大意: 给定一个$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);
}
最新文章
- js传入参数为字符串问题
- 相识HTML5 canvas
- PHP数组操作
- JavaScript闭包之“词法作用域”
- MyBatis foreach标签遍历数组
- javascript bind
- session和cookie的区别和联系
- Unity5网络模块UNet介绍
- Cow Bowling
- MotionEvent
- divmod(a,b)函数
- CentOS7 lamp安装 centoOS6 lamp
- vue2项目使用axios发送请求
- 201521123080《Java程序设计》第5周学习总结
- C - The C Answer (2nd Edition) - Exercise 1-7
- 微信小程序腾讯云php后台解决方案
- bzoj 1485 [HNOI2009]有趣的数列 卡特兰数
- C# 生成随机索引列表
- Dev_VGridControl的使用
- octave基本操作
热门文章
- Link-Cut Tree(LCT) 教程
- HDU 6089 Rikka with Terrorist (线段树)
- Apicloud_(项目)网上书城03_拓展模块实现
- JS框架_(Qrcode.js)将你的内容转换成二维码格式
- webpack3升级webpack4
- docker容器的学习
- Boston House Price with Scikit-Learn
- 关于Oracle报表
- Ubuntu16.04小白入门分享之 玩转Ruby你需要安装什么软件(持续更新)
- 基于form表单的极验滑动验证小案例