B - Discovering Gold

B - Discovering Gold

You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold.

Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice after throwing, you add X to your position and collect all the gold from the new position. If your new position is outside the cave, then you keep throwing again until you get a suitable result. When you reach the Nth position you stop your journey. Now you are given the information about the cave, you have to find out the expected number of gold you can collect using the given procedure.

Input

Input starts with an integer T (****≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the dimension of the cave. The next line contains N space separated integers. The ith integer of this line denotes the amount of gold you will get if you come to the ith cell. You may safely assume that all the given integers will be non-negative and no integer will be greater than 1000.

Output

For each case, print the case number and the expected number of gold you will collect. Errors less than 10-6 will be ignored.

Sample Input

3

1

101

2

10 3

3

3 6 9

Sample Output

Case 1: 101.0000000000

Case 2: 13.000

Case 3: 15

题意:

有n个格子,编号分别是1~N,每个格子下面有黄金,每到一个格子就掷骰子,掷到的点数就是你下一次跳跃的距离,骰子的有6面,点数分别为1~6,每次掷的点数是等概率的,如果你掷色子使你跳到第N格外面,则重新掷骰子。问到达n点的所获得黄金的期望是多少?

分析:

D P [ i ] 1 i 不能简单的用DP[i]代表1到i的期望 DP[i]1i 如果对之有疑问请看下面,否则建议跳过

d p [ n ] 1 n . 用dp[n]代表1到n点所获的期望的话,则不能单纯的让前置位置的期望加上该点的黄金再乘以的到该状态的概率. dp[n]1n.

对于 d p [ j ] dp[j] dp[j], j j j的每个前置顶点 i i i加进来的期望为 d p [ j ] + = ( d p [ i ] + v a l [ i <mtext>   </mtext> t o <mtext>   </mtext> j ] ) P ( 1 <mtext>   </mtext> t o <mtext>   </mtext> i ) P ( i <mtext>   </mtext> t o <mtext>   </mtext> j ) dp[j]+=(dp[i]+val[i\ to\ j ])*P(1\ to\ i)*P(i\ to \ j) dp[j]+=(dp[i]+val[i to j])P(1 to i)P(i to j)

P ( 1 <mtext>   </mtext> t o <mtext>   </mtext> i ) ! = 1 而P(1\ to\ i)!=1 P(1 to i)!=1 , 1 i i 因为1去的方向最终点不全在i点,即所有可能的最终点不在i出, 1ii

d p [ i ] i n d p [ i ] : 用dp[i]代表i到n点的期望,那么dp[i]的求法: dp[i]indp[i]:

d p [ i ] = j ( v a l [ i ] + d p [ j ] ) P ( i <mtext>   </mtext> t o <mtext>   </mtext> j ) dp[i]=\sum _{j}(val[i]+dp[j])*P(i\ to\ j)​ dp[i]=j(val[i]+dp[j])P(i to j)

<mtext>   </mtext> P ( i <mtext>   </mtext> t o <mtext>   </mtext> j ) 注意\ P(i\ to\ j)的求法即可  P(i to j)

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e2+10;
int val[maxn];
double dp[maxn];
int main()
{
    int t,n,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d",val+i);
        dp[n]=val[n];
        int tot=0;
        for(int i=n-1;i>0;--i)
        {
            tot=min(i+6,n)-i;//可供选择的个数
            dp[i]=val[i];
            for(int j=i+tot;j>i;--j)
                dp[i]+=1.0/tot*dp[j];
        }
        printf("Case %d: %.6f\n",++cas,dp[1]);
    }
}

##如果用DP[i]表示1到i的期望,则代码这样写

include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e2+10;
int val[maxn];
double dp[maxn];
double pp[maxn];
int main()
{
    int t,n,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d",val+i);
        int tot;
        dp[1]=val[1];
        pp[1]=1;
        for(int i=2;i<=n;++i)
        {
            dp[i]=0;
            pp[i]=0;
            for(int j=max(i-6,1);j<i;j++)
            {
                 tot=min(j+6,n)-j;
                 dp[i]+=(dp[j]+val[i]*pp[j])*1.0/tot;
                 pp[i]+=pp[j]*1.0/tot;
            }
        }
        printf("Case %d: %.6f\n",++cas,dp[n]);
    }
    return 0;
}
全部评论

相关推荐

LuminousZJ:不行,最后还是要看学信网的,这点不能伪装,也骗不过人家,得不偿失
点赞 评论 收藏
分享
1.&nbsp;自我介绍2.&nbsp;项目都是自己写的吗?3.&nbsp;我看你用&nbsp;koa2&nbsp;写后端,为什么选择它,能讲讲吗?4.&nbsp;那你提到&nbsp;koa2&nbsp;它是不提供中间件的,你是怎么解决的?5.&nbsp;中间件的原理是什么?(洋葱模型)6.&nbsp;你刚刚说碰到&nbsp;next()&nbsp;就进入下一个中间件,那&nbsp;next&nbsp;只能执行同步,如果是异步的话,你是怎么处理的?(async/await,但是我发现,有的中间件需要在异步中间件之前执行,所以我用&nbsp;try/catch&nbsp;来处理异步中间件的异常)7.&nbsp;JS&nbsp;异步发展史,以及它们的优缺点说一下&nbsp;(回调函数--Promise--Generator--async/await)8.&nbsp;你刚刚说&nbsp;Promise&nbsp;状态不能更改,那如果我要设计一个能修改&nbsp;Promise&nbsp;状态的函数,你会怎么设计?9.&nbsp;CSS&nbsp;水平垂直居中的方法(flex、grid、绝对定位&nbsp;+&nbsp;margin:auto、绝对定位&nbsp;+&nbsp;负&nbsp;margin、绝对定位&nbsp;+&nbsp;transform、table-cell)10.&nbsp;你刚刚说到&nbsp;flex&nbsp;布局,那&nbsp;flex:1&nbsp;是什么意思?(flex:&nbsp;flex-grow&nbsp;&nbsp;flex-shrink&nbsp;&nbsp;flex-basis;等价&nbsp;flex:1&nbsp;1&nbsp;0%表示元素可以均分剩余空间,可拉伸、可压缩,不依赖内容宽度,自动自适应填充布局。)11.&nbsp;父容器宽是&nbsp;500px,然后它左右各有两个子容器是&nbsp;100px,如果设置&nbsp;flex:&nbsp;1,那它的宽度是多少?(500-100-100=300px)12.&nbsp;说说你对浏览器缓存的理解(强缓存、协商缓存)13.&nbsp;如果一个用户,他怎么去刷新都无法刷到最新版的代码,你能说下可能的原因吗?(版本号、hash等)还有吗?(我说我不知道了,面试官说还有&nbsp;CDN&nbsp;没有同步,我说企业才会这么干,自己写项目一般不会,我知道&nbsp;cdn&nbsp;是用来解决高并发的手段)14.&nbsp;React你熟吗?说下&nbsp;React&nbsp;函数组件和类组件的区别15.&nbsp;怎么避免&nbsp;Hooks&nbsp;导致组件重新渲染?(使用&nbsp;useCallback、useMemo、React.memo、useRef等等)16.&nbsp;谈一下我对&nbsp;React&nbsp;的状态管理的理解(Redux、Mobx、Zustand,我说&nbsp;Zustand&nbsp;用的最多)17.&nbsp;React&nbsp;常见的&nbsp;hooks&nbsp;有哪些?(useState、useEffect、useRef、useCallback、useMemo、useReducer、useContext、useImperativeHandle、useLayoutEffect、useDebugValue)18.&nbsp;TS&nbsp;你熟吗?我们引进&nbsp;TS&nbsp;的目的是为什么?19.&nbsp;interface&nbsp;和&nbsp;type&nbsp;的区别20.&nbsp;说下&nbsp;TS&nbsp;里的泛型21.&nbsp;我现在有十个字段,比如十个字段就要&nbsp;A&nbsp;B&nbsp;C&nbsp;D&nbsp;E&nbsp;F&nbsp;G&nbsp;这种。那我现在另有另外一个方法,这个方法接受的参数呢,必须是这个&nbsp;interface&nbsp;A&nbsp;里面的这个&nbsp;K。就比如说你可以是&nbsp;A&nbsp;B&nbsp;C&nbsp;可以&nbsp;A&nbsp;B&nbsp;C&nbsp;D&nbsp;任何组合都可以,但是必须是这个&nbsp;interface&nbsp;里面的&nbsp;A&nbsp;里面的定义的。这个&nbsp;K&nbsp;这种类型的话是怎么去定义呢?(说实话我有点不太理解啥意思,反正我说了&nbsp;keyof)```&nbsp;TypeScriptinterface&nbsp;Obj&nbsp;{A:&nbsp;stringB:&nbsp;stringC:&nbsp;stringD:&nbsp;stringE:&nbsp;string//&nbsp;其他字段...}```22.&nbsp;vite&nbsp;用过吗?说说和&nbsp;webpack&nbsp;的区别。vite&nbsp;的优缺点是什么23.&nbsp;说说&nbsp;Tree&nbsp;shaking(树摇)&nbsp;和&nbsp;Code&nbsp;Splitting&nbsp;(代码分割)的区别24.&nbsp;Git&nbsp;你熟吗?说说&nbsp;git&nbsp;merge&nbsp;和&nbsp;git&nbsp;rebase&nbsp;的区别,什么时候用&nbsp;git&nbsp;merge,什么时候用&nbsp;git&nbsp;rebase?25.&nbsp;web3&nbsp;你熟吗?(不太熟,听说过而已)26.&nbsp;我看你自我介绍说了&nbsp;AI,你是怎么用的?27.&nbsp;除了提示词,还有什么能让&nbsp;AI&nbsp;更聪明?28.&nbsp;AI&nbsp;的优缺点你说一下29.&nbsp;AI&nbsp;发展这么快,你觉得我们以后会扮演什么角色?30.&nbsp;反问基本都答上来了。面了我80分钟,我还以为稳过的
查看29道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务