BZOJ——1419: Red is good

http://www.lydsy.com/JudgeOnline/problem.php?id=1419

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1077  Solved: 491
[Submit][Status][Discuss]

Description

桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元。可以随时停止翻牌,在最优策略下平均能得到多少钱。

Input

一行输入两个数R,B,其值在0到5000之间

Output

在最优策略下平均能得到多少钱。

Sample Input

5 1

Sample Output

4.166666

HINT

输出答案时,小数点后第六位后的全部去掉,不要四舍五入.

Source

f[r][b]表示选r个红牌 b个黑牌在 最优策略 平均 能得到的钱

f[r][b]=max( 0, (f[r-1][b]+1)*r/(r+b)+(f[r][b-1]-1)*b/(r+b) )

//当前选一张红牌的概率为r/(r+b),红牌钱数+1,期望为f[r-1][b]+1)*r/(r+b),选黑牌类似

但是T了。。。然后、、需要用滚动数组优化第一维、

 1 #include <cstdio>
 2 
 3 #define max(a,b) (a>b?a:b)
 4 
 5 inline void read(int &x)
 6 {
 7     x=0; register char ch=getchar();
 8     for(; ch>'9'||ch<'0'; ) ch=getchar();
 9     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
10 }
11 
12 const int N(5e3+5);
13 
14 double f[N];
15 int R,B;
16 
17 int Presist()
18 {
19     read(R),read(B);
20     for(int r=1; r<=R; ++r)
21     {
22         f[0]=r;
23         for(int b=1; b<=B; ++b)
24               f[b]=max(0.00,1.*(f[b]+1)*r/(r+b)+
25                             1.*(f[b-1]-1)*b/(r+b));
26     }
27     printf("%.6lf
",f[B]-5e-7);
28     return 0;
29 }
30 
31 int Aptal=Presist();
32 int main(int argc,char**argv){;}
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7859153.html