题目大意:给你一个$[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. 二十一、proxyDesign 代理模式

    原理: 时序图: 代码清单: Printable public interface Printable { void setPrinterName(String name); String getPr ...

  2. Es6(Symbol,set,map,filter)

    首先再讲这几个新东西之前,先说一个Es6中新出的扩展运算符(...) 1.展开运算符,就是把东西展开,可以用在array和object上 比如: let a=[,] let b=[,...a,]//[ ...

  3. react官方脚手架搭建项目

    1.全局安装 npm install -g create-react-app 2. app后面还要给项目文件命名 create-react-app //是全局命令来创建react项目 3.然后按照提示 ...

  4. 187. Repeated DNA Sequences重复的DNA子串序列

    [抄题]: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &qu ...

  5. 2018上IEC计算机高级语言(C)作业 第1次作业

    1.经过这几周的学习,总结一下学习的心得与体会.(不少于100字:10分) 学习c语言已经一个学期了,刚开始学习的时候老是感觉力不从心.虽然认真听课了, 但是并不能理解它.这种情况到了后来才有所改变. ...

  6. poj1284

    一个欧拉函数的应用,当时也没有太搞清,这里直接用的当时的模板 #include<iostream> #include<cstdlib> #include<cstdio&g ...

  7. Java8特性之Lambda、方法引用和Streams

    这里涉及三个重要特性: Lambda 方法引用 Streams ① Lambda 最早了解Lambda是在C#中,而从Java8开始,Lambda也成为了新的特性,而这个新的特性的目的,就是为了消除单 ...

  8. Alpha项目冲刺

    一.团队成员 学号 姓名 211606361 何承华(队长) 211606356 陈宇 211606360 丁培辉 211606333 温志铭 211606343 杨宇潇 211606391 张主强 ...

  9. Html5与Css3知识点拾遗(七)

    布局 实例:规范的命名和编排 <body> <div class="page"><!--page开始--> <header class=& ...

  10. STM32外设初始化步骤

    1.定义外设结构体: 2.开启外设时钟: 3.调用缺省值配置函数: 4.外设具体配置: 5.外设使能.