概率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);
}

 

posted @   北岛知寒  阅读(228)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩