【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 ...
随机推荐
- 初学c# -- 学习笔记 小结
学了到了好些东西, 做了一些练习. 一.C# winform Socket 1.程序主要部分只是用了 Panel.Picturebox.Label.RicheditBox四个组件,滚动条.编辑框什么的 ...
- netty(六) websocket开发应用
package com.lance.net.server.common; import java.net.InetSocketAddress; import org.springframework.s ...
- Linux 子网掩码计算, 二进制十进制互相转换
看下边例子 192.168.0.1/24 192.168.0.1/32 192.168.0.1/28 上边24,32,28对应的掩码都是什么,怎么计算的 24,32,28,对应的就是多少个二进制的1 ...
- Linux上web服务器搭建
安装php依赖包: yum -y install gcc gcc++ libxml2 libxml2-devel yum install gcc make gd-devel libjpeg-devel ...
- linux服务器系统负载监控-shell脚本
一.监控服务器系统负载情况: 1.用uptime命令查看当前负载情况(1分钟,5分钟,15分钟平均负载情况) # uptime 15:43:59 up 186 days, 20:04, 1 us ...
- HDU 5828 Rikka with Sequence(线段树区间加开根求和)
Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...
- Head First Servlets & JSP 学习笔记 第六章 —— 会话状态
MVC中的M(模型),通常就是一个普通的类,这个类里面的信息就是业务逻辑. 会话(Session) 我们可以使用一个HttpSession对象,来保存横跨多个请求的会话状态. HTTP协议使用的是无状 ...
- Vmware unknow Interface ens33
vmare打开虚拟网络编辑器,按图示操作
- php Pthread 多线程 Worker
<?php //PHP 高级编程之多线程 http://www.netkiller.cn/journal/thread.php.html#idp57489856 //worker 是一个具有持久 ...
- 转载-js如何设置网页横屏和竖屏切换
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...