[CF 335E]Counting Skyscrapers题解翻译

跳跃表大小估算算法与应用

题目翻译:


题解翻译:


这道题中的摩天大楼描述了被称作跳跃表(Skip List)的数据结构。跳跃表在某种意义上和AVL、红黑树相似,因为它支持O(logN)的插入,操作和查找(包括上下界查询),同时支持常数时间的在排序顺序中前后移动。跳跃表和任何树结构都不同,因为它有实用的,不需要线程锁的(所谓‘无锁’),线程安全的实现方式。无锁跳跃表被用作MemSQL中主要的存储数据结构,并且Bob使用的,穿过摩天大楼的算法是一种O(logN)的方法,它能让人们估算跳跃表的大小。因此,如果Bob最终得到的数值是n,则跳跃表真实大小的期望值就是n(因此如果输入文件的第一行是Bob,你就只需要读入第二行的数并输出它)。形式化的证明十分简单,我们把它留给读者。奇怪的是,逆命题并不成立。如果跳跃表的大小是n,那么估算算法的期望返回值比n要大。对于一个远大于2^h的n,估算值收敛于n-1+2^h,但这个问题同时还包含了n较小的数据。


让我们每次将解的层数增加1.当H=0时,期望的估算值自然是N。现在对于加上的每一层,我们都可以加上这一层溜索的期望花费,并同时减去它们下方溜索的期望花费。最终我们将得到总共的期望花费。


对于高度为H的某一层,让我们考虑左侧和右侧的一些塔,并计算它们之间存在一条溜索的可能性。令L是两座塔之间的距离。一条可能存在的,长度为L,高度为H的溜索的确存在,如果它两端的塔都足够地高(每个都有1/(2^H)的概率),并且中间的L-1座塔都较低(每个都有1-(1/(2^H))的概率)。因此这样一条溜索存在的概率是(1/(2^2H))*(1-(1/(2^H))^(L-1))。


现在,假设这样一条溜索存在,它下方溜索的期望数量是多少?它就是高度H-1的塔的数量加1。L-1座塔中的每一个都有1/(2^H-1)的概率高度为H-1(注意到它的高度至多是H-1)——运用条件概率公式P(A|B)=P(A∩B)/P(B)来计算这个。因此一个长度为L,高度为H的溜索下方溜索的期望数量是1+(L-1)/(2^H-1)。


对于每个长度L,在每一层都有N-L条长度为L的可能溜索。对于所有可能的溜索,我们把它出现的概率乘以它的花费,求和得到总的期望值。


因而最终答案就是:



事实上内层循环可以用矩阵乘法计算,运行时间为O(HlogN)。——虽然数据范围足够小,用矩阵乘法是高射炮打蚊子——O(H*N)就行了。


译者注1:

以下是对Bob情形的证明,即题解中“留给读者”的部分:

假设Bob即将走一条高度为h的溜索(此时他所在的大楼高度>=h),这条溜索的长度期望是多少?

此时Bob站在这条溜索的左端点,而未知的部分只有溜索越过的大楼和溜索的右端点。换句话说,这条溜索期望增加多少栋大楼?

容易发现,某栋大楼高度>=h的概率是1/(2^h)。即,在2^h栋楼中才期望出现一个高度>=h的楼。因而,这条高度为h的溜索的期望长度是2^h。

也就是说,一条花费2^h的溜索的期望长度是2^h。由于Bob的计数器一开始是1,所以很容易得出,大楼数的期望就是计数器最终的值n。


译者注2:

以下是对“高度为H-1的概率为1/(2^H-1)”的证明:

显然我们已经知道这栋楼的高度<H。因此我们实际要求的是P(高度为H-1 | 高度<H)(在高度<H的前提下高度为H-1的概率)。

根据条件概率公式,P(高度为H-1 | 高度<H) = P(高度为H-1且高度<H) / P(高度<H) = P(高度为H-1) / P(高度<H) = (1/(2^(H-1))) / (1-(1/(2^H))) = 1/(2^H-1)。


译者注3:

为什么只需要减去高度为H-1的溜索呢?因为对于这条高度为H的溜索,其两端大楼的高度都>=H,因而在上一个阶段中高度都>=H-1,因此在上一个阶段,这两栋楼之间只存在高度为H-1的溜索。


代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
char name[20];
int N,H;
int main(){
	scanf("%s",name);
	scanf("%d%d",&N,&H);
	if(name[0]=='B'){
		printf("%d\n",N);
		return 0;
	}
	double po=1,ans=0;
	for(int i=1;i<=H;i++){
		po*=2;
		double t=1;
		for(int j=1;j<=N;j++){
			ans+=(N-j)/po/2*t*(po-j)/(po-1);
			t*=1-1/po;
		}
	}
	printf("%.10lf\n",ans+N);
	return 0;
}


WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/d1/24/0794fe23b1a762ae03ff8db91b10de5999436ffc8bc24cf96e264e24a1e6/vector_quantize_pytorch-1.16.1-py3-none-any.whl (39 kB) INFO: pip is still looking at multiple versions of optimum to determine which version is compatible with other requirements. This could take a while. WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/42/fa/2836732a62b4899fcbd1f5e26ec42c4a8494f399e9d2ad8b3a7551983c2c/vector_quantize_pytorch-1.15.6-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/d8/60/d1989a9d958c893ffc6136cd0dbb5490911fea3e15833c2420dab178935b/vector_quantize_pytorch-1.15.5-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/5b/86/6513dfba2928f1690d04466a95912ad526b8e1a134be1ab7b6ca335e2779/vector_quantize_pytorch-1.15.4-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/f4/79/f8f7700cd86411b8253d824a46132e7091ec95acda517fb4c33345c16dfc/vector_quantize_pytorch-1.15.3-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/77/5c/1e230ac0a4ef760d35ebaf83ee1fc63603fabd4921acfd16ca7037f8f605/vector_quantize_pytorch-1.15.2-py3-none-any.whl (38 kB) INFO: This is taking longer than usual. You might need to provide the dependency resolver with stricter constraints to reduce runtime. See https://pip.pypa.io/warnings/backtracking for guidance. If you want to abort this run, press Ctrl + C. WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/42/b7/6d5ad02a029d15246c6b4884290a06fe6521d5e17a22c12289e926427ff3/vector_quantize_pytorch-1.15.1-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/96/2c/f90a03d658435adc5fb15671bcf791d0426c7279b02a4c41e65e48410641/vector_quantize_pytorch-1.15.0-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/73/32/8af32fad23d4f7d48e0c65d35c187953e64ea8d5f39e6e3abb8d108d320f/vector_quantize_pytorch-1.14.46-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/fb/db/169761922a4c8c47470b42a09c0870da82ae2e82c8067d1f108f18e52a1c/vector_quantize_pytorch-1.14.45-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/ee/b5/ddb332365492d81bd06e1d8bac390633af9dbc4bb85870bca43dd4cae14a/vector_quantize_pytorch-1.14.44-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/e7/14/c9bdcdb4c47e09fc055ac8bda3519c228a11a08c36e2936305348d1fa415/vector_quantize_pytorch-1.14.43-py3-none-any.whl (38 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Using cached https://pypi.tuna.tsinghua.edu.cn/packages/6e/29/ed997e56331dbc677be2028a49662a2a226c9855887d071631dfc39c31f2/vector_quantize_pytorch-1.14.42-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/58/00/73aa281d6a7dfed26d4adce6d8dd4b7ceb8b0f805f174a66e942c590b81c/vector_quantize_pytorch-1.14.41-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0d/9c/24a2890d2900640c70194791a2da77e83c7556395c55b6e7eb6f27f1d5e8/vector_quantize_pytorch-1.14.40-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/11/2e/b8568351d1c9547b547bc9941bef4a2391caf0063a585086dbb23fba602a/vector_quantize_pytorch-1.14.39-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/94/f9/ed1b780c18e559684b9bba0c39de0de00708cd87fffd5d25d98b65840a9e/vector_quantize_pytorch-1.14.37-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/67/0d/0b74350dc61fbd88cf5350ac4bac1d779ee50e7be4288533358f90fe16de/vector_quantize_pytorch-1.14.36-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f2/cf/1e0591894027b6ea6e763553f3470a93d388514dce18dfe254052ee4373c/vector_quantize_pytorch-1.14.35-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5a/13/55dfe5494bdbc9dfc31c308b1faea1fd56b62d007f6d4b3ce5b7bcdcdb8a/vector_quantize_pytorch-1.14.34-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/03/8c/c599b06f0340009faae0fe17276735cd780744c8a6a6dde7292d20b709ce/vector_quantize_pytorch-1.14.33-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f5/da/737d9792ebd3efa5203b5eb5090305c25aac11a20092645c56808b9f2e0c/vector_quantize_pytorch-1.14.32-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f2/18/ad8a08fc9b2ee612a31c35ef0012a918735c4a1d6a937aa6c703076ff121/vector_quantize_pytorch-1.14.31-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c6/f8/4f214d994368807dba65cb56817e03553376991da4f17b2936c4159c774c/vector_quantize_pytorch-1.14.30-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/77/4a/7d9238bb61489d85343335c89fad94123de9571ec788c7f4b8c170186ebd/vector_quantize_pytorch-1.14.29-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e8/62/dbd487f64156a9c083117333a055fe31a710b054a1526ab48e95934497c7/vector_quantize_pytorch-1.14.28-py3-none-any.whl (37 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all' Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4d/6d/e747eb10d5b0ea7fe5c90f6745957e5d55f23ec05f734f426026bd77c323/vector_quantize_pytorch-1.14.27-py3-none-any.whl (36 kB) WARNING: typer 0.16.0 does not provide the extra 'all' WARNING: typer 0.16.0 does not provide the extra 'all'
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值