题目大意

给定一个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. Angular/cli的安装

    Angular cli 是一个命令行工具,用来创建,打包,发布项目. Angular cli 安装之前必须先安装Node 6.9.0及以上版本,NPM 3 及以上版本. 在cmd控制台窗口执行命令no ...

  2. 配置intellij idea中的欢迎页而不使用默认的index.jsp

    在web.xml中添加 <welcome-file-list> <welcome-file>abc.jsp</welcome-file> </welcome- ...

  3. Linux下使用指定网卡进行ping操作

    目录   1. 为什么要使用知道那个网卡ping操作   2. 使用指定网卡ping操作   3. 总结 1. 为什么要使用指定网卡ping操作 现在很多服务器都拥有双网卡甚至多网卡,有些是为了保持高 ...

  4. LNMP+HAProxy+Keepalived负载均衡 - 基础服务准备

    日志服务 修改日志服务配置并重启日志服务: ``` vim /etc/rsyslog.conf ``` 编辑系统日志配置,指定包含的配置文件路径和规则: ``` $IncludeConfig /etc ...

  5. React路由-进阶篇

    路由进阶 1.多级路由,和之前的思想一样,在子路由里面继续写Route,继续挂载组件,就可以实现多级路由 比如这样:class Food extends Component{ render() { r ...

  6. json_encode 的小技巧

    做了一个 API 文档自动生成,解析的是每个 控制器类 的注释 json 数据,在做测试工具的时候,多层的 json 只有通过一个 textarea 把数据弄进去.如下图 怎么格式化 Json 数据并 ...

  7. 处理nginx访问日志,筛选时间大于1秒的请求

    #!/usr/bin/env python ''' 处理访问日志,筛选时间大于1秒的请求 ''' with open('test.log','a+',encoding='utf-8') as f_a: ...

  8. HDU3853 概率DP

    LOOPS   Homura wants to help her friend Madoka save the world. But because of the plot of the Boss I ...

  9. 在WebAPI中调用其他WebAPI

    client.BaseAddress = new Uri("http://xxx.xxx.xx.xx:xxxx/); client.DefaultRequestHeaders.Accept. ...

  10. POJ:3320-Jessica's Reading Problem(尺取法)

    Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15224 Accepted: ...