uva 10090 So you want to be a 2n-aire?

來源:互聯網
上載者:User

原題:
The player starts with a prize of $1,
and is asked a sequence of n ques-
tions. For each question, he may
• quit and keep his prize.
• answer the question.
If wrong, he quits with nothing.If correct, the prize is doubled,and he continues with the next question.After the last question, he quits with his prize. The player wants to maximize his expected prize.Once each question is asked, the player is able to assess the probability p that he will be able to answer it. For each question, we assume that p is a random variable uniformly distributed over the range t..1.
Input
Input is a number of lines, each with two numbers: an integer 1 ≤ n ≤ 30, and a real 0 ≤ t ≤ 1. Input
is terminated by a line containing ‘0 0’. This line should not be processed.
Output
For each input n and t, print the player’s expected prize, if he plays the best strategy. Output should
be rounded to three fractional digits.
Sample Input
1 0.5
1 0.3
2 0.6
24 0.25
0 0
Sample Output
1.500
1.357
2.560
230.138
大意:
在一個電視娛樂節目中,你一開始有1元錢。主持人會問你n個問題,每次你聽到問題後有兩個選擇:一是放棄回答該問題,離開遊戲,拿走獎金;二是回答問題。如果回答正確,獎金加倍;如果回答錯誤,遊戲結束,你一分錢也拿不到。如果正確地回答完所有n個問題,你將拿走所有的2 n 元錢,成為2 n 元富翁。當然,回答問題是有風險的。每次聽到問題後,你可以立刻估計出答對的機率。由於主持人會隨機問問題,你可以認為每個問題的答對機率在t和1之間均勻分布。輸入整數n和實數t(1≤n≤30,0≤t≤1),你的任務是求出在最優策略下,拿走的獎金金額的期望值。這裡的最優策略是指讓獎金的期望值盡量大。
代碼:

#include <bits/stdc++.h>using namespace std;double PowTwo[31],t;int n;int main(){    ios::sync_with_stdio(false);    PowTwo[0]=1;    for(int i=1;i<=30;i++)        PowTwo[i]=PowTwo[i-1]*2;    double ex;    while(cin>>n>>t,n+t)    {        if(abs(1-t)<1e-9)        {            cout<<fixed<<setprecision(3)<<PowTwo[n]<<endl;            continue;        }        ex=PowTwo[n];        for(int i=n-1;i>=0;i--)        {            double p0=PowTwo[i]/ex;            if(p0<=t)                ex=(1+t)/2*ex;            else            {                double p1=(p0-t)/(1-t);                ex=PowTwo[i]*p1+(1+p0)/2*ex*(1-p1);            }        }        cout<<fixed<<setprecision(3)<<ex<<endl;    }    return 0;}

解答:
這題不會做,偶然發現在劉汝佳的紫書講解數學期望的例題上面就是這道題。此題真是好題啊,而且不太好想。
下面解答來自劉汝佳的紫書。
假設你剛開始遊戲,如果直接放棄,獎金為1;如果回答,期望獎金是多少呢?不僅和
第1題的答對機率p相關,而且和答後面的題的情況相關。即:
選擇“回答第1題”後的期望獎金 = p * 答對1題後的最大期望獎金
注意,上式中“答對1題後的最大期望獎金”和這次的p無關,這提示我們用遞推的思想,
用d[i]表示“答對i題後的最大期望獎金”,再加上“不回答”時的情況,可以得到:若第1題答對
機率為p,期望獎金的最大值 = max{2 0 , p*d[1]}
這裡故意寫成2 0 ,強調這是“答對0題後放棄”所得到的最終獎金。
上述分析可以推廣到一般情況,但是要注意一點:到目前為止,一直假定p是已知的,
而p實際上並不固定,而是在t~1內均勻分布。根據連續機率的定義,d[i]在概念上等於
max{2^i ,p*d[i+1]}在p=t~1上的積分。不要害怕“積分”二字,因為雖然在概念上這是一個積
分,但是落實到具體的解法上,仍然只需要基礎知識。
因為有max函數的存在,需要分兩種情況討論,即p×d[i+1]<2 i 和p×d[i+1]≥2 i 兩種情況。
令p 0 =max{t, 2 i /d[i+1]}(加了一個max是因為根據題目,p≥t),則:
p < p0時,p×d[i+1]<2 i ,因此“不回答”比較好,期望獎金等於2 i 。
p≥p0時,“回答”比較好,期望獎金等於d[i]乘以p的平均值(d[i]作為常數被“提出來”了),即(1+p0)/2 ×d[i+1]。(取平均值的實際上就是算均勻分布的期望,算出來的期望值是獲勝機率的期望值)
在第一種情況中,p的實際範圍是[t,p0),因此機率為p1=(p0-t)/(1-t)。根據全期望公
式,d[i] = 2 i × p1 + (1+p0)/2 × d[i+1] × (1-p1)。
邊界是d[n] = 2 n ,逆向遞推出d[0]就是本題的答案。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.