LightOJ 1030 - Discovering Gold - [概率DP]
题目链接:https://cn.vjudge.net/problem/LightOJ-1030
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,每个格子里有价值Gi的宝藏;
现在,扔一个六面的标准的骰子,按得到的点数走格子,起点为1;
如果在某个格子,扔出骰子之后,得到的点数会让你走到编号大于n的格子,就不算数,重新扔,直到扔到一个你能走点数为止;
如果你走到了编号n的格子,就停止;
求得到宝藏价值的期望值。
题解:
每个格子的价值为Gi,我们只要求出每个格子可能被走到的概率Pi,那么我们求出Σ( Gi * Pi )即为答案;
显然是个概率DP题,动态转移求出走到每个格子的概率即可;
AC代码:
#include<cstdio>
#include<cstring>
#define MAXN 105
#define min(a,b) (a<b)?a:b
int n,grid[MAXN];
double dp[MAXN];
int main()
{
int t;
scanf("%d",&t);
for(int kase=;kase<=t;kase++)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",grid+i); memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<=n;i++)
{
int k=min(,n-i);
for(int j=;j<=k;j++) dp[i+j]+=dp[i]*(1.0/k);
} double ans=;
for(int i=;i<=n;i++) ans+=dp[i]*grid[i];
printf("Case %d: %.7lf\n",kase,ans);
}
}
PS. becky大佬有期望DP的做法:http://blog.csdn.net/becky_w/article/details/78247858
LightOJ 1030 - Discovering Gold - [概率DP]的更多相关文章
- LightOJ 1030 Discovering Gold (概率/期望DP)
题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...
- LightOj 1030 - Discovering Gold(dp+数学期望)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...
- Light OJ 1030 - Discovering Gold(概率dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...
- LightOJ - 1030 Discovering Gold —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold PDF (English) Statistics For ...
- LightOJ 1030 Discovering Gold (期望)
https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...
- LightOJ 1030 Discovering Gold(概率DP)题解
题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望 思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] ...
- LightOJ 1030 Discovering Gold(期望 概率)
正推,到达i的概率为p[i],要注意除了1和n外,到达i的概率并不一定为1 概率表达式为p[i] += p[j] / min(n - j, 6) 从j带过来的期望为exp[i] += exp[j] / ...
- 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 ...
- LightOJ 1030 Discovering Gold
期望,$dp$. 设$ans[i]$为$i$为起点,到终点$n$获得的期望金币值.$ans[i]=(ans[i+1]+ans[i+2]+ans[i+3]+ans[i+4]+ans[i+5]+ans[i ...
随机推荐
- android 不能在子线程中更新ui的讨论和分析
问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...
- Java动态调用类中方法
在Java中,调用类的方法有两种方式:对于静态方法可以直接使用类名调用,对于非静态方法必须使用类的对象调用.反射机制提供了比较另类的调用方式,可以根据需要指定要调用的方法,而不必在编程时确定.调用的方 ...
- GoF--装饰者设计模式
顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例. 装饰器模式的应用场景: 1.需要扩展一个类的功能. 2.动态的为 ...
- Git Step by Step – (7) Git远程仓库(续)
上一篇文章介绍了Git远程仓库的一些使用,但是还是有些东西需要补充一下,所以有了这个续篇. .gitignore 前一篇中,我们介绍了Git的patch功能,当我们生成patch之后,"gi ...
- windows防火墙设置端口开放技巧
选择“打开或者关闭windows防火墙”把防火墙打开,然后选择“高级设置”,选择“创建规则”来指定端口.(这里也可以在“入站规则”里选择已经存在的端口.) 指定ip开放3389端口 某新服务器,开放8 ...
- 使用 Beautiful Soup
Beautiful Soup 用法: (1) 前面我们爬取一个网页,都是使用正则表达式来提取想要的信息,但是这种方式比较复杂,一旦有一个地方写错,就匹配不出来了,因此我们可以使用 Beautiful ...
- C++ template —— trait与policy类(七)
第15章 trait与policy类---------------------------------------------------------------------------------- ...
- jquery on=>keyup无法绑定未来js添加的元素
$("[name=red_count]").on("keyup", countKeyUpBind);即使使用on的,也无法绑定未来元素, 所以直接在动态添加的时 ...
- JS 运行、复制、另存为 代码。
//运行代码 function runEx(cod1) { cod = document.getElementById(cod1) var code = cod.value; if (code != ...
- c++ 类内static成员初始化
类内部的static成员,除了为const static 且为整数类型(int char bool)可在类内部初始化. 其他的都建议在对应的cpp文件中进行初始化. test.h #ifndef TE ...