【codeforces 335E】 Counting Skyscrapers

http://codeforces.com/problemset/problem/335/E (題目連接)ios

題意

  懶得寫了= =spa

Solution

  這題咋不上天= =。.net

  參考題解:http://blog.csdn.net/clover_hxy/article/details/62217674code

  對於第一問,有另外一種比較容易懂的想法:blog

假設Bob即將走一條高度爲h的溜索(此時他所在的大樓高度>=h),這條溜索的長度指望是多少?get

此時Bob站在這條溜索的左端點,而未知的部分只有溜索越過的大樓和溜索的右端點。換句話說,這條溜索指望增長多少棟大樓?string

容易發現,某棟大樓高度>=h的機率是1/(2^h)。即,在2^h棟樓中才指望出現一個高度>=h的樓。於是,這條高度爲h的溜索的指望長度是2^h。it

也就是說,一條花費2^h的溜索的指望長度是2^h。因爲Bob的計數器一開始是1,因此很容易得出,大樓數的指望就是計數器最終的值n。io

——wmdcstdioclass

細節

  ?

代碼

// 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;
}
相關文章
相關標籤/搜索