大意: 给定一个$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);
}

最新文章

  1. js传入参数为字符串问题
  2. 相识HTML5 canvas
  3. PHP数组操作
  4. JavaScript闭包之“词法作用域”
  5. MyBatis foreach标签遍历数组
  6. javascript bind
  7. session和cookie的区别和联系
  8. Unity5网络模块UNet介绍
  9. Cow Bowling
  10. MotionEvent
  11. divmod(a,b)函数
  12. CentOS7 lamp安装 centoOS6 lamp
  13. vue2项目使用axios发送请求
  14. 201521123080《Java程序设计》第5周学习总结
  15. C - The C Answer (2nd Edition) - Exercise 1-7
  16. 微信小程序腾讯云php后台解决方案
  17. bzoj 1485 [HNOI2009]有趣的数列 卡特兰数
  18. C# 生成随机索引列表
  19. Dev_VGridControl的使用
  20. octave基本操作

热门文章

  1. Link-Cut Tree(LCT) 教程
  2. HDU 6089 Rikka with Terrorist (线段树)
  3. Apicloud_(项目)网上书城03_拓展模块实现
  4. JS框架_(Qrcode.js)将你的内容转换成二维码格式
  5. webpack3升级webpack4
  6. docker容器的学习
  7. Boston House Price with Scikit-Learn
  8. 关于Oracle报表
  9. Ubuntu16.04小白入门分享之 玩转Ruby你需要安装什么软件(持续更新)
  10. 基于form表单的极验滑动验证小案例