[BZOJ 1419] Red is good

[题目链接]

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

[算法]

       概率DP

       用Fi,j表示还剩下i张红卡 , j张黑卡 , 期望获得的最大价值

       时间复杂度 : O(N ^ 2)

       滚动数组 , 将空间复杂度降至O(N)

[代码]

        

#include<bits/stdc++.h>
using namespace std;
#define MAXN 5010
#define RG register

int R , B;
double dp[2][MAXN];

template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}
int main()
{
        
        read(R); read(B);
        dp[0][0] = 0;
        for (RG int i = 0; i <= R; i++)
        {
                for (RG int j = 0; j <= B; j++)
                {
                        if (i + j == 0) continue;
                        double p = 1.0 * i / (i + j);
                        if (i == 0) dp[i & 1][j] = max(0.0 , (dp[i & 1][j - 1] - 1) * (1 - p));
                        else if (j == 0) dp[i & 1][j] = max(0.0 , (dp[(i - 1) & 1][j] + 1) * p);
                        else dp[i & 1][j] = max(0.0 , 1.0 * (dp[(i - 1) & 1][j] + 1) *  p + 1.0 * (dp[i & 1][j - 1] - 1) * (1 - p));
                }
        }
        dp[R & 1][B] *= (int)1e6;
        dp[R & 1][B] = floor(dp[R & 1][B]);
        dp[R & 1][B] /= (int)1e6;
        printf("%.6lf\n" , dp[R & 1][B]);
        
        return 0;
    
}

 

posted @ 2018-11-02 20:57  evenbao  阅读(140)  评论(0编辑  收藏  举报