Flipping Coins Gym - 101606F

版权声明:本文为博主原创文章,欢迎转载。 https://blog.csdn.net/Smiler_/article/details/79875896

题意:Here’s a jolly and simple game: line up a row of N identical coins, all with the heads facingdown onto the table and the tails upwards, and for exactly K times take one of the coins, toss itinto the air, and replace it as it lands either heads-up or heads-down. You may keep all of thecoins that are face-up by the end.Being, as we established last year, a ruthless capitalist, you have resolved to play optimally towin as many coins as you can. Across all possible combinations of strategies and results, whatis the maximum expected (mean average) amount you can win by playing optimally?

给你一些硬币,硬币的初始状态都是正面向下,反面向上,现在你可以每次任取一个硬币把它抛向空中,你可以进行k次操作,问你k次操作之后硬币正面向上的最大数学期望。

思路:一共进行k次操作,每次我们都可以任取一个硬币把它抛一次,由于题目中说是让求最大的数学期望,那么我们抛的时候取正面向下的可以得到最大的数学期望,我们定义一个状态dp[i][j],表示抛i次其中有j个硬币正面向上的概率。

那么我们可以得到转移方程:

dp[i+1][j]+=dp[i][j]*0.5;

dp[i+1][j+1]+=dp[i][j]*0.5;

当j=n时

dp[i+1][n]+=dp[i][n]*0.5;

dp[i+1][n-1]+=dp[i][n]*0.5;

注意dp[0][0]=1;

#include <bits/stdc++.h>
using  namespace  std;
const int maxn=450;
double dp[maxn][maxn];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0; i<maxn; i++)
        for(int j=0; j<maxn; j++)
            dp[i][j]=0;
    dp[0][0]=1;
    for(int i=0; i<k; i++)
    {
        for(int j=0; j<n; j++)
        {
            dp[i+1][j]+=dp[i][j]*0.5;
            dp[i+1][j+1]+=dp[i][j]*0.5;
        }
        dp[i+1][n]+=dp[i][n]*0.5;
        dp[i+1][n-1]+=dp[i][n]*0.5;
    }
    double ans=0;
    for(int i=1; i<=n; i++)
        ans+=(dp[k][i]*i);
    printf("%.10f\n",ans);


}


猜你喜欢

转载自blog.csdn.net/Smiler_/article/details/79875896