概率dp - Uva 10900 So you want to be a 2n-aire?
So you want to be a 2n-aire?
Problem's Link
Mean:
玩一个答题赢奖金的游戏,一开始有1块钱,玩n次,每次赢的概率为t~1之间的某个实数.
给定n和t,求最终能够获得奖金的最大期望值.
analyse:
假设玩家已经答对了第i题,那么当前的奖金应该为2^i.
现在开始第i+1题,是选择放弃还是答题呢?
设答第i+1题正确的概率为p,ex[i+1]表示考虑前1~i+1题最大的收益期望.
如果p*ex[i+1]>2^i,那么肯定是选择答;否则结束游戏.
由此可得,临界概率为bp=2^i/ex[i-1].
画一条[0,1]的数轴,讨论bp和t的关系.
如果t>bp,显然p*ex[i+1]>2^i成立,此时ex[i+1]=ex[i]*(1+t)/2;
否则,需要分两段来考虑:
此时:ex[i+1] = (bp-t)/(1-t) * 2^i + (1-bp)/(1-t) * (1+bp)/2 * ex[i];
Time complexity: O(N)
view code
/*
* -----------------------------------------------------------------
* Copyright (c) 2015 crazyacking All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Verdict: Accepted
* Submission Date: 2014-06-18-16.39
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define max(a,b) (a>b?a:b)
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
int n=0;
double ex[30+5]= {0.0},dp[30+5]= {0.0},t=0.0,bp=0.0;
int main()
{
dp[0]=1;
for(int i=1; i<=30; i++)
dp[i]=dp[i-1]*2;
while(scanf("%d%lf",&n,&t)!=EOF&&(n>0))
{
ex[n]=dp[n];
for(int i=n-1; i>=0; i--)
{
bp=dp[i]/ex[i+1];
if(bp<=t)
ex[i]=(1+t)/2 * ex[i+1];
else
ex[i]=(bp-t)/(1-t) * dp[i] + (1-bp)/(1-t) * (1+bp)/2 * ex[i+1];
}
printf("%.3lf\n",ex[0]);
}
return(0);
}
* -----------------------------------------------------------------
* Copyright (c) 2015 crazyacking All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Verdict: Accepted
* Submission Date: 2014-06-18-16.39
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define max(a,b) (a>b?a:b)
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
int n=0;
double ex[30+5]= {0.0},dp[30+5]= {0.0},t=0.0,bp=0.0;
int main()
{
dp[0]=1;
for(int i=1; i<=30; i++)
dp[i]=dp[i-1]*2;
while(scanf("%d%lf",&n,&t)!=EOF&&(n>0))
{
ex[n]=dp[n];
for(int i=n-1; i>=0; i--)
{
bp=dp[i]/ex[i+1];
if(bp<=t)
ex[i]=(1+t)/2 * ex[i+1];
else
ex[i]=(bp-t)/(1-t) * dp[i] + (1-bp)/(1-t) * (1+bp)/2 * ex[i+1];
}
printf("%.3lf\n",ex[0]);
}
return(0);
}