抛硬币 Flipping Coins(Gym - 101606F)

Posted kang000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抛硬币 Flipping Coins(Gym - 101606F)相关的知识,希望对你有一定的参考价值。

Here’s a jolly and simple game: line up a row of N identical coins, all with the heads facing
down onto the table and the tails upwards, and for exactly K times take one of the coins, toss it
into the air, and replace it as it lands either heads-up or heads-down. You may keep all of the
coins that are face-up by the end.
Being, as we established last year, a ruthless capitalist, you have resolved to play optimally to
win as many coins as you can. Across all possible combinations of strategies and results, what
is the maximum expected (mean average) amount you can win by playing optimally?
Input
One line containing two space-separated integers:
  N(1<=N<=400), the number of coins at your mercy;
  K(1<=K<=400), the number of flips you must perform.
Output
Output the expected number of heads you could have at the end, as a real number. The output
must be accurate to an absolute or relative error of at most 10-6.
技术分享图片

----------------------------------------------------------------------------------------------------------------------

题意:给出N个硬币,开始均反面朝上。每次挑出其中一个抛,连续K次,求正面朝上的最大数学期望。
----------------------------------------------------------------------------------------------------------------------

由于是求最大数学期望,所以每次抛硬币即要优先选择反面硬币

所以只有两种挑选硬币的情况:

  1.正面数量为 0 ~ n-1 ,选择反面硬币抛,抛出结果正面数量比原本 +1 或 不变

  2.正面数量为 n,只能够选择正面硬币抛,抛出结果正面数量比原本 -1 或 不变
----------------------------------------------------------------------------------------------------------------------

设 dp[i][j] 表示: 第 i 次抛硬币后, j 个硬币正面朝上的概率

  1.当 j < n 时,dp[i][j]的概率一分为二,各给dp[i+1][j]dp[i+1][j+1],即

for(int j=0;j<n;j++){
    dp[i+1][j]+=dp[i][j]/2;
    dp[i+1][j+1]+=dp[i][j]/2;
}

  2.当 j == n 时,dp[i][j]的概率一分为二,各给dp[i+1][j]dp[i+1][j-1],即

dp[i+1][n]+=dp[i][n]/2;
dp[i+1][n-1]+=dp[i][n]/2;

如此即可求出n个硬币抛k次的各个正面朝上的概率,最后求数学期望即可

附:n=2,k=4时的dp转移表格:

技术分享图片

技术分享图片
 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <queue>
 7 #include <stack>
 8 #include <functional>
 9 #define INF 0x3f3f3f3f
10 using namespace std;
11 typedef long long ll;
12 double dp[410][410], ans;
13 int main(){
14     int n, k;
15     while (~scanf("%d %d", &n, &k)){
16         for (int i = 0; i <= 400; i++)
17             for (int j = 0; j <= 400; j++)
18                 dp[i][j] = 0;
19         dp[0][0] = 1;
20         for (int i = 0; i < k; i++){
21             for (int j = 0; j < n; j++){
22                 dp[i + 1][j] += dp[i][j] * 0.5;
23                 dp[i + 1][j + 1] += dp[i][j] * 0.5;
24             }
25             dp[i + 1][n] += dp[i][n] * 0.5;
26             dp[i + 1][n - 1] += dp[i][n] * 0.5;
27         }
28         ans = 0;
29         for (int i = 1; i <= n; i++)
30             ans += i*dp[k][i];
31         printf("%.8lf\\n", ans);
32     }
33     return 0;
34 }
AC代码

 



以上是关于抛硬币 Flipping Coins(Gym - 101606F)的主要内容,如果未能解决你的问题,请参考以下文章

HDU5985 Lucky Coins 概率dp

HDU 5985 Lucky Coins(概率)

概率DP 2017 ICPC 乌鲁木齐 A Coins

zoj 2949 - Coins of Luck

ACM-ICPC 2017 Asia Urumqi A. Coins期望dp

[LeetCode] Arranging Coins 排列硬币