首页 > 其他 > 详细

概率dp

时间:2018-01-20 21:08:15      阅读:551      评论:0      收藏:0      [点我收藏+]

题目:http://codeforces.com/gym/101606

Problem F Flipping Coins
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 ?ips 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.
Sample Input 1 Sample Output 1 2 1 0.5
Sample Input 2 Sample Output 2 2 2 1
Sample Input 3 Sample Output 3 2 3 1.25
Sample Input 4 Sample Output 4 6 10 4.63476563
Sample Input 5 Sample Output 5 6 300 5.5

题意:

输入n个硬币和投n次求最多硬币正面朝上的期望

ps;当所有面朝上时还会继续投

思路:dp(表示只会这一种dp)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#define maxn 200005
using namespace std;
int main()
{
    int n,b;
    cin>>n>>b;
    double dp[405][405];
    dp[0][0]=1;
    for(int i=1;i<=b;i++)
        for(int j=0;j<=i;j++){
    if(j<n)
    {

        dp[i][j]+=dp[i-1][j]/2;
        dp[i][j+1]+=dp[i-1][j]/2;
    }
    else if(j==n)
    {
        dp[i][j-1]+=dp[i-1][j]/2;
        dp[i][j]+=dp[i-1][j]/2;
    }
  }
  double ans=0;
     for(int i=0;i<=n;i++)
            ans+=dp[b][i]*i;
        printf("%.8lf\n",ans);
  return 0;
}

 

 

这里分享n种概率dp

http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710606.html

 

概率dp

原文:https://www.cnblogs.com/huangzzz/p/8321805.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!