D - Discovering Gold

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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

题目大意:抛色子移动,每个地点都有一些金子,问你到到达终点的时候拿到的金子数量的数学期望。

思路分析:刚开始始终都不懂题意,后来百度了一下才知道是让去求期望,但是依然没有什么好的思路,

后来认真的去复习了一些有关数学期望的姿势,知道读到这一句,解决这类问题,对随机变量A、B,有

数学期望E(aA+bB)=aE(A)+bE(b);根据这个不正可以构建状态转移方程用DP做么,每一点的期望可以

由它之前的位置的点的期望求出,DP[i]=a[i]+1/step*dp[i-(1...step)]

但是我现在不太理解的是为什么一定要逆推而不能正推orz,求指教,想明白了以后我也会回来补上。

代码:

#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=110;
double dp[maxn],a[maxn];
int kase=0;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lf",&a[i]);
        memset(dp,0,sizeof(dp));
        dp[n]=a[n];
        for(int i=n-1;i>=1;i--)
        {
            dp[i]=a[i];
            //cout<<dp[i]<<endl;
            int step=min(6,n-i);
            for(int j=1;j<=step;j++)
            {
                dp[i]+=1.0/(step*1.0)*dp[i+j];
            }
        }
        printf("Case %d: %.6lf\n",++kase,dp[1]);
    }
    return 0;
}

最新文章

  1. Cookie/Session机制详解
  2. [Unity] 在协程中等待指定的毫秒
  3. java-int类型:int默认为0导致更新操作未赋值的情况下将值更新为0
  4. 理解Javascript之执行上下文(Execution Context)
  5. Wix 安装部署教程(八) 自动生成XML小工具
  6. 18位身份证验证--java实现,正则表达式
  7. WEB前端的原理及组成
  8. (转)CSS 为不同大小的浏览器视窗使用不同的样式表
  9. js运动:分享到
  10. Writing the first draft of your science paper — some dos and don’ts
  11. mysql处理上百万条的数据库如何优化语句来提高处理查询效率
  12. android host
  13. pl/sql oracle
  14. linux下分割文件
  15. OC中的类别Category-协议Protocol-…
  16. Java基础10:全面解读Java异常
  17. php学习----错误处理和代码重用
  18. ESP8266串口和MQTT服务器消息互传(版本一) 单纯透传+保存WIFI账号信息
  19. LeetCode(95): 不同的二叉搜索树 II
  20. seq2seq和attention应用到文档自动摘要

热门文章

  1. 字符串编码---hash函数的应用
  2. [Leetcode][021] Merge Two Sorted Lists (Java)
  3. 【vc】5_文本编程
  4. js学习笔记——数组方法
  5. android studio gradle自动签名构建实现
  6. 怎样制作百度recovery【转】
  7. 将主机IDS OSSEC日志文件存入MYSQL的方法
  8. IC卡接口芯片TDA8007的读写器设计
  9. 直接修改别人jar包里面的class文件 工具:jclasslib
  10. javascript学习笔记——chrome等提示找不到“getElementsByTagName”的一种解决方法