传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030

Discovering Gold

Time Limit: 2 second(s) Memory Limit: 32 MB

Program 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

Output for Sample Input

Case 1: 101.0000000000

Case 2: 13.000

Case 3: 15


解题心得:

  1. 考的就是一个期望dp ,求期望一个很重要的就是逆求期望,为啥是逆求呢,如果正求是在前面期望的基础之上求期望。期望的期望只是一种可能性,并不符合概率要求。这个可以参考贝叶斯公式的定义,里面说的很清楚。关于求期望,要从已知推到未知,就这个题来说,已知只能是必定到达最后一个格子。所以要从已知走向位置就是逆着求的。
  2. dp[i]代表的是扔到第i个格子期望得到多少的金子。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
double dp[maxn];
int num[maxn],t,n;
int main()
{
scanf("%d",&t);
int T = 1;
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
dp[i] = num[i];
}
int count = 1;
for(int i=n-1;i>=1;i--)
{
if(count > 6)//在后面没有六个格子可能性就没有六种
count = 6;
for(int j=1;j<=count;j++)
dp[i] += (double)(dp[i+j]/count);
count++;
}
printf("Case %d: ",T++);
printf("%.7f\n",dp[1]);
}
}

LightOj:1030-Discovering Gold(期望dp模板)的更多相关文章

  1. LightOJ - 1030 Discovering Gold —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold    PDF (English) Statistics For ...

  2. LightOJ 1030 - Discovering Gold - [概率DP]

    题目链接:https://cn.vjudge.net/problem/LightOJ-1030 You are in a cave, a long cave! The cave can be repr ...

  3. LightOj 1030 - Discovering Gold(dp+数学期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...

  4. LightOJ 1030 Discovering Gold(期望 概率)

    正推,到达i的概率为p[i],要注意除了1和n外,到达i的概率并不一定为1 概率表达式为p[i] += p[j] / min(n - j, 6) 从j带过来的期望为exp[i] += exp[j] / ...

  5. LightOJ 1030 Discovering Gold (概率/期望DP)

    题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...

  6. LightOJ 1030 Discovering Gold(期望)

    Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...

  7. LightOJ 1030 Discovering Gold(概率DP)题解

    题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望 思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] ...

  8. LightOJ 1030 Discovering Gold (期望)

    https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...

  9. LightOJ 1030 Discovering Gold 数学期望计算

    题目大意:给出长度为n的一条隧道,每个位置都有一定数量的财宝.给你一枚骰子,roll到几点就前进几步,如果即将到达的地方超过了这条隧道长度,就重新roll一次,走到n点结束.求这个过程能收获多少财宝. ...

  10. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...

随机推荐

  1. 批量插入,update

    #####setting 1create table t as select * from all_objects where 1 =2; ###.模拟逐行提交的情况,注意观察执行时间DECLAREB ...

  2. docker postgresql FATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied

    在docker中启动postgresql时出现错误 FATAL:  could not access private key file "/etc/ssl/private/ssl-cert- ...

  3. D. Restructuring Company 并查集 + 维护一个区间技巧

    http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...

  4. Windows7&IIS7.5部署Discuz全攻略

    组长说在内网部署一个论坛,这可难不倒我,装个Discuz嘛.部署环境就一台普通的PC,四核i3,Windows7.这就开搞了. 准备工作 系统是Windows 7 专业版,自带IIS7.5(家庭版不带 ...

  5. Could not load file or assembly 'Oracle.ManagedDataAccessDTC.DLL' or one of its dependencies.

    Could not load file or assembly 'Oracle.ManagedDataAccessDTC.DLL' or one of its dependencies.  不是有效的 ...

  6. java获取服务器一些信息的方法

    request.getServletContext().getRealPath("/") 获取项目所在服务器的全路径,如:D:\Program Files\apache-tomca ...

  7. jdk1.6与jdk1.7list集合排序区别与算法

    源码分析: 在Collections.sort中:    public static <T extends Comparable<? super T>> void sort(L ...

  8. 疯狂使用 leancloud (投稿文章)

    疯狂使用 leancloud 本文章是投稿文章,已在 leancloud 微信公众号发表. 这里是原文,内容有调整. 3年,从工程师到创始人 觉得不错可以点这里进行 leancloud 注册 项目背景 ...

  9. 扒一扒IT大佬高考:马云数学1分考北大 李彦宏是状元

    http://news.cnblogs.com/n/522622/ 高考今天正式拉开序幕,而像李彦宏.马云等 IT 大佬之前也都参加过高考,他们成绩又都是怎样的呢? 马化腾:放弃天文梦选择计算机 20 ...

  10. HDU 2147 kiki's game kiki的游戏(博弈,找规律)

    题意: 给一个有n*m 个格子的棋盘,将一个硬币放在右上角一格,每次可以往左/下/左下移动一格,碰到不能移动的局面者输. 思路: 找P/N状态.先将(n,1)归为P状态,那么能一步到达此位置的有3个位 ...