【xsy2332】Randomized Binary Search Tree DP+FFT
题目大意:给你一个$[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的更多相关文章
- 【XSY2332】Randomized Binary Search Tree 概率DP FFT
题目描述 \(\forall 0\leq i<n\),求有多少棵\(n\)个点,权值和优先级完全随机的treap的树高为\(i\). \(n\leq 30000\) 题解 设\(f_{i,j}\ ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【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 ...
- 【题解】【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 ...
- 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告
时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: ...
- 【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 ...
随机推荐
- Excel、VBA与MySQL交互
本文主要涉及: VBA中的MySQL环境配置 VBA连接MySQL数据库 VBA读写MySQL数据 在Excel中连接MySQL数据库及数据读写 系统环境: Windows 10 Excel 2013 ...
- 十三、Visitor 访问者设计模式
需求:将数据结果与处理分开 设计原理: 代码清单: Element public interface Element { void accept(Visitor visitor); } Entry p ...
- NTFS(Windows)、ext4(RHEL6)和xfs(RHEL7)文件系统的误删除恢复和备份
前言 对于误删除文件的设备,要马上停止任何写的操作,防止删除的文件被覆盖,导致数据丢失! 恢复NTFS文件系统下误删的文件 以Windows为例,市面上能恢复的工具不少,例如EasyRecovery. ...
- c#: Label控件加入AutoHeight属性
此功能在界面布局中颇为实用,录代码以记之: public class LabelEx : Label { private bool autoHeight = true; [DefaultValue(t ...
- https 网络传输安全架设
1:在集群的情况下,不能在tomcat上 架构ssl 而是在总路由nginx上架设具体实现如下截图 非对称加密是当前流行的加密传输方式 证书是什么 . 在浏览器证书查看 证书是访问请求时 https ...
- 电子商务系统+java+web+完整项目+包含源码和数据库Java实用源码
鸿鹄云商大型企业分布式互联网电子商务平台,推出PC+微信+APP+云服务的云商平台系统,其中包括B2B.B2C.C2C.O2O.新零售.直播电商等子平台. 分布式.微服务.云架构电子商务平台 java ...
- 分布式协议学习笔记(三) Raft 选举自编写代码练习
由于时间安排上的原因,这次的代码写的稍微有些简略,只能算是自己对RAFT协议的一个巩固. 实现定义2个节点,使用读取配置文件来获取IP和端口以及节点ID 网络使用boost同步流程 一个线程收 一个线 ...
- finereport 下拉复选框多选
- delphi三层结构常出现的问题和解决方案
以下问题出现原因有可能多个,暂时将我遇见的记录下来,以后有新的在陆续更新上去,有网友愿意的话也可以共同测试一下. 一,无法更新定位行.一些值可能已在最后一次读取已更改. 错误出现前提: 1, 录数据时 ...
- Vue中出现Do not use built-in or reserved HTML elements as component id:footer等等vue warn问题
错误示图: 原因:是因为在本地项目对应文件的<script>中,属性name出现了错误的命名方式,导致浏览器控制台报错! 诸如: name: header . . name: menu ...