zoukankan      html  css  js  c++  java
  • Discovering Gold lightoj 1030 (dp+期望)

    http://acm.hust.edu.cn/vjudge/contest/128683#problem/C

    题意:现在有n个格子,每个格子上都有一定的黄金值;还有一个色子(1-6)。起始位置站在格子1上面,若每次投掷色子得到数x,x+i<=n(i表示现处位置的格子编号),则可以到达(x+i)格子上;反之,再进行一次投掷。问:到达标号为n的格子上面,得到黄金的期望值是多少?

    期望:

    一件不确定的事件有确定的所有结果,把第一种的结果值记为s1,它发生的概率记为p1,第二种结果值记为s2,它发生的概率为p2,... 第n种结果值记为sn,它发生的概率记为pn ... 那么期望值 Ei= s1*p1 + s2*p2 +... + sn*pn + ...

    分析:

    以第三个例子为例:

    3

    3 6 9

    若我们现处在格子1,那么E1=3+6*1/2+9*1/2

    (既然我们已经处在格子1了,1格子的黄金我们确定可以拿走了。剩下只有两种情况,要么到达格子2,要么到达格子3,所以两者概率分别为1/2。剩下的情况同理)

    若我们现处在格子2,那么E2=6+9

    若我们现处在格子3,那么E3=9

     

    但是现在还有一个条件是色子只有6面(1-6),所以当n大于6时,比如

    n=8,

    E1=s1+s2*p2+s3*p3+s4*p4+s5*p5+s6*p6+s7*p7,

    E2=s2+s3*p3+s4*p4+s5*p5+s6*p6+s7*p7+s8*p8。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <stack>
    #include <math.h>
    
    using namespace std;
    
    #define INF 0x3f3f3f3f
    const int maxn = 110;
    typedef long long LL;
    double dp[maxn];
    
    int main()
    {
        int T, n, num, cnt=1;
    
        scanf("%d", &T);
    
        while(T --)
        {
            scanf("%d", &n);
    
            for(int i=1; i<=n; i++)
            {
                scanf("%d", &num);
                dp[i] = num;
            }
    
            for(int i=n-1; i>=1; i--)
            {
                int x = min(6, n-i);///dp[i]最多只能从dp[i+7]的位置得来
    
                for(int j=1; j<=x; j++)
                {
                    dp[i]+=dp[i+j]*1.0/x;
                }
    
            }
    
           printf("Case %d: %.7lf
    ",cnt++, dp[1]);
    
        }
        return 0;
    }
    /*
    1
    8
    1 1 1 1 1 1 1 1
    */
    View Code
  • 相关阅读:
    [SHOI2015]零件组装机
    [AH2017/HNOI2017]影魔
    空指针RE第一次公开赛-笔记
    i春秋2020新春公益赛WP
    博客园Markdown编辑器修改代码配色、添加代码行号
    buuctf Writeup
    关于Tarjan的一些问题
    NOIP2013D1T3货车运输 (生成树+树链剖分)
    1051: [HAOI2006]受欢迎的牛 (tarjan强连通分量+缩点)
    CodeForces 438D The Child and Sequence (线段树 暴力)
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5788115.html
Copyright © 2011-2022 走看看