题目大意

给定一个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. leetcode笔记(二)94. Binary Tree Inorder Traversal

    题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...

  2. 屏蔽datatable错误提示

    $.fn.dataTable.ext.errMode = 'none'; //不显示任何错误信息// 以下为发生错误时的事件处理,如不处理,可不管.$('#productionRequestItems ...

  3. 基于WSAAsyncSelect模型的两台计算机之间的通信

    任务目标 编写Win32程序模拟实现基于WSAAsyncSelect模型的两台计算机之间的通信,要求编程实现服务器端与客户端之间双向数据传递.客户端向服务器端发送"请输出从1到1000内所有 ...

  4. PHP自动生成分页链接

    page.class.php <?php class Page { // 分页栏每页显示的页数 public $rollPage = 5; // 页数跳转时要带的参数 public $param ...

  5. Python容器--list, tuple, dict, set

    ## Python 中有四种用于存放数据的序列--list, tuple, dict, set ## list 列表 - 可以存放任意类型数据的有序序列 - 列表可以由零个或多个元素组成,元素之间用逗 ...

  6. python中函数的不定长参数

    例1: #定义一个含有不定长参数的函数,本例第三个参数*args def sum_nums(a,b,*args): print('_'*30) print(a) print(b) print(args ...

  7. C语言字符篇(五)内存函数

    memcpy不可以把目的地址写成本身 但是memmove可以,因为它是先保存到临时空间 #include <string.h>   void *memcpy(void *dest, con ...

  8. cf978E Bus Video System

    The busses in Berland are equipped with a video surveillance system. The system records information ...

  9. HTML中body相关标签-03

    今日主要内容: 列表标签 <ul>.<ol>.<dl> 表格标签 <table> 表单标签 <fom> 一.列表标签 列表标签分为三种. 1 ...

  10. CSS3 Flexbox(伸缩盒/弹性盒模型)可视化指南

    在http://css.doyoe.com/(CSS参考手册)中,本文对应其中的伸缩盒 引入 Flexbox布局官方称为CSS Flexible Box Layout Module是一个CSS3新的布 ...