题目大意:给你一个$[0,1]$之间等概率随机序列,你需要把这个序列插入到一棵$treap$中,问这棵$treap$的期望深度,请对于$[1,n]$中的每个深度分别输出它的概率(实数,保留五位小数)。

$treap$的优先级之也是在$[0,1]$中等概率随机出来的。

ps:这个$[0,1]$的随机非常$niubi$,任意一个$[0,1]$间的实数被选中的概率是$0$

这一题有一个很特殊的性质:两个序列都是等概率随机出来的。

在没有相同数的情况下,我们发现$treap$最终的形态跟数值插入的顺序是没有关系的。

假如我们将整个序列排序,我们会发现这些元素被$treap$分配的优先级,是一个(随机的)随机数序列。

$treap$的形状等价于优先级序列的笛卡尔树。

所以我们可以列出一个$dp$:设$f[i][j]$表示$j$个点构成的笛卡尔树深度不大于$i$的概率。

考虑到每个点的值都是随机的,则有:$f[i][j]=\frac{1}{j}\sum\limits_{k=1}^{j}f[i-1][k-1]\times f[i-1][j-k]$

直接转移的概率显然是$O(n^3)$的。

我们发现,概率会集中在笛卡尔树期望深度附近,所以实际上并不需要DP到$f[n][]$,我们大概率只需要dp到$f[50][]$即可满足精度要求(剩下的都是0)

然后,发现上面的式子是一个卷积,我们可以用$FFT$加速转移。

所以最终的复杂度就变成了$O(An\log\  n)$

 #include<bits/stdc++.h>
#define M (1<<16)
#define PI acos(-1)
using namespace std; struct cp{
double r,i; cp(){i=r=;}
cp(double R,double I){r=R; i=I;}
friend cp operator +(cp a,cp b){return cp(a.r+b.r,a.i+b.i);}
friend cp operator -(cp a,cp b){return cp(a.r-b.r,a.i-b.i);}
friend cp operator *(cp a,cp b){return cp(a.r*b.r-a.i*b.i,a.r*b.i+a.i*b.r);}
friend cp operator /(cp a,double b){return cp(a.r/b,a.i/b);}
}a[M]; void change(cp a[],int n){
for(int i=,j=;i<n-;i++){
if(i<j) swap(a[i],a[j]);
int k=n>>;
while(j>=k) j-=k,k>>=;
j+=k;
}
}
void FFT(cp a[],int n,int on){
change(a,n);
for(int h=;h<=n;h<<=){
cp wn=cp(cos(*PI/h),sin(*PI*on/h));
for(int j=;j<n;j+=h){
cp w=cp(,);
for(int k=j;k<j+(h>>);k++){
cp u=a[k],t=a[k+(h>>)]*w;
a[k]=u+t; a[k+(h>>)]=u-t;
w=w*wn;
}
}
}
if(on==-)
for(int i=;i<n;i++) a[i]=a[i]/n;
} int main(){
int n; scanf("%d",&n); double las=;
int len=; for(;len<=n*;len<<=);
a[]=cp(,);
for(int hh=;hh<=min(n,);hh++){
//if(las<1e-5){printf("0\n"); continue;}
FFT(a,len,);
for(int i=;i<len;i++) a[i]=a[i]*a[i];
FFT(a,len,-);
for(int i=n;i<len;i++) a[i]=cp(,);
for(int i=n;i;i--) a[i]=a[i-]/i; a[]=cp(,);
printf("%.10lf\n",a[n].r-las);
las=a[n].r;
}
for(int i=min(n,)+;i<=n;i++) printf("%.10lf\n",);
}

【xsy2332】Randomized Binary Search Tree DP+FFT的更多相关文章

  1. 【XSY2332】Randomized Binary Search Tree 概率DP FFT

    题目描述 \(\forall 0\leq i<n\),求有多少棵\(n\)个点,权值和优先级完全随机的treap的树高为\(i\). \(n\leq 30000\) 题解 设\(f_{i,j}\ ...

  2. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  3. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  4. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. 【leetcode】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  6. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  7. 【题解】【BST】【Leetcode】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告

    时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: ...

  9. 【leetcode】 Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. OpenStack 安装:glance 安装

    接上一篇keystone, 这一篇介绍glance服务: 在开始操作之前,先用source环境变量,然后创建glance 用户,并设置密码为glance [root@linux-node1 ~]#op ...

  2. 利用iptables防火墙允许1521端口被连接

    今天在虚拟机上装了ora11g , 出现2个问题 1.i386的几个RPM包缺少导致安装验证不通过 2.安装完成后,本地电脑连不上虚拟机oracle , 但是能ping通 问题2: 简单的方法就是关掉 ...

  3. 在Laravel中使用mongoDB

    https://blog.csdn.net/weixin_38682852/article/details/80840678?utm_source=blogxgwz1 https://blog.csd ...

  4. HDU 6301 Distinct Values

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6301 多校contest1 题目大意是有一个长度为N的数组,给出M个"事实",每个 ...

  5. python 迭代器生成

    博客:http://www.cnblogs.com/alex3714/articles/5765046.html 列表生成式 [i*2 for i in range(10)] #创建时就生成,不调用也 ...

  6. Python local error

    原来在python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,在修改之前对该变量的引用自然就会出现没定义这样的错误了,如果确定要引用全局变量,并且要对它修改,必须加上global关 ...

  7. FortiGate 5.2/5.4 SSLVPN建立

    1.定义源IP池 即用户通过sslvpn拨号成功后获取到的IP地址. 2.定义路由地址 即用户通过sslvpn拨号成功后允许获取到的路由表. 3.建立sslvpn portal 4.定义用户和用户组 ...

  8. Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/jms/JMSContext

    参考链接 : https://blog.csdn.net/angus_Lucky/article/details/82811946?utm_source=blogxgwz7 org.springfra ...

  9. logback log4j log4j2 性能实测

    logback log4j log4j2 性能实测 转载: https://blog.souche.com/logback-log4j-log4j2shi-ce/ 日志已经成为系统开发中不可或缺的一部 ...

  10. MVC API 返回json 对象,使用netjson 返回

    1.清除xml 格式 GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); 2. ...