bzoj1419: Red is good
题目链接
题解
由于有决策存在,所以要满足最有子结构,而期望正是能用来表示改状态的好坏
继续翻牌期望<0莫不如不取
设dp[i,j]表示有i张+1,j张-1的牌
dp[i,j]=max(0,(dp[i-1,j]+1)\frac{i}{i +j} + (dp[i,j-1]-1)\frac{j}{i+j})
dp[i,0]=i;
随便滚掉一维,空间就能过了
代码
#include<cstdio>
#include<algorithm>
inline int read() {
int x = 0;
char c = getchar();
while(c < '0' || c > '9')c = getchar();
while(c <= '9' && c >= '0')x = x * 10 + c - '0',c = getchar();
return x;
}
const int maxn = 5007;
int R,B;
double dp[2][maxn];
int main() {
R = read();B = read();
//for(int i = 1;i <= R;++ i) dp[i][0] = (double) i;
int k = 1;
for(int i = 1;i <= R;++ i) {
k ^= 1;
dp[k][0] = i;
for(int j = 1;j <= B;++ j) {
dp[k][j] = std::max((dp[k ^ 1][j] + 1.0) * ((double) i /(i + j)) + (dp[k][j - 1] - 1.0) * ((double) j / (i + j)),0.0);
}
}
long long tmp = dp[k][B] * 1000000;
printf("%.6lf\n",(double) tmp / 1000000);
return 0;
}