【codeforces 335E】 Counting Skyscrapers

http://codeforces.com/problemset/problem/335/E (题目链接)

题意

  懒得写了= =

Solution

  这题咋不上天= =。

  参考题解:http://blog.csdn.net/clover_hxy/article/details/62217674

  对于第一问,有另一种比较容易懂的想法:

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

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

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

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

——wmdcstdio

细节

  ?

代码

// codeforces 335E
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;
//learn from clover_hxy
char ch[10];
int n,h;

int main() {
	scanf("%s",ch);
	scanf("%d%d",&n,&h);
	if (ch[0]=='B') {printf("%d",n);return 0;}
	double ans=n;double p=1;
	for (int i=1;i<=h;i++) {
		p*=2;double t=1;
		for (int j=1;j<=n;j++,t*=(1-1/p))
			ans+=(n-j)/p/p*t*(p-p/2*(1+(j-1)/(p-1)));
	}
	printf("%.9lf",ans);
	return 0;
}

 

posted @ 2017-03-24 15:05  MashiroSky  阅读(298)  评论(0编辑  收藏  举报