LightOj:1030-Discovering Gold(期望dp模板)
传送门: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
解题心得:
- 考的就是一个期望dp ,求期望一个很重要的就是逆求期望,为啥是逆求呢,如果正求是在前面期望的基础之上求期望。期望的期望只是一种可能性,并不符合概率要求。这个可以参考贝叶斯公式的定义,里面说的很清楚。关于求期望,要从已知推到未知,就这个题来说,已知只能是必定到达最后一个格子。所以要从已知走向位置就是逆着求的。
- 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模板)的更多相关文章
- LightOJ - 1030 Discovering Gold —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold PDF (English) Statistics For ...
- 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 ...
- LightOj 1030 - Discovering Gold(dp+数学期望)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...
- 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 (概率/期望DP)
题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...
- 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)题解
题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望 思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] ...
- LightOJ 1030 Discovering Gold (期望)
https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...
- LightOJ 1030 Discovering Gold 数学期望计算
题目大意:给出长度为n的一条隧道,每个位置都有一定数量的财宝.给你一枚骰子,roll到几点就前进几步,如果即将到达的地方超过了这条隧道长度,就重新roll一次,走到n点结束.求这个过程能收获多少财宝. ...
- Light OJ 1030 - Discovering Gold(概率dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...
随机推荐
- java中存储金额
很早之前, 记得一次面试, 面试官问存储金钱用什么数据类型? 当时只知道8种数据类型(boolean, byte, short, int, long, float, double, char)的我, ...
- JavaSE---基本网络支持
1.Java为网络支持提供了java.net包,该包下的URL和URLConnection等类提供了 以编程方式访问WEB服务 的功能: 2.URLDecoder.URLEncoder提供了 普通字符 ...
- Redis的数据类型(Strings、 hashes)
字符串(Strings)类型及操作 字符串是Redis值的最基础的类型,一个key对应一个value,Redis字符串是二进制安全的,这意味着一个Redis字符串可以包含任何种类的数据,例如一个JPE ...
- WebStorm技巧-在安卓手机上运行Ionic程序
打开菜单项 Run -> Run- 选择 Edit Configurations- 添加一个 PhoneGap/Cordova 配置项,命名如: Ionic Android, 并输入相关 ...
- ASPCTJ
ublic interface ISomeService { public void doSome(); public String dade(); } public class SomeServic ...
- Java thymeleaf模板获取资源文件的内容
我们在某些时候可能需要获取配置文件properties中的配置信息,而不需要用Java传给模板,在模板中就可以直接获取 我们需要在resources/下定义国际化配置文件即可,注意名称必须中messa ...
- 函数补充:动态参数,函数嵌套,global与nonlocal关键
一丶动态参数 1.*args 位置参数,动态传参 def func(*food): print(food) print(func("米饭","馒头"," ...
- 地址栏传值 JS取值方法
function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...
- Problem O: 国家排序
Problem O: 国家排序 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 405 Solved: 253[Submit][Status][Web ...
- angular设置反向代理
本地调试,需要用到服务器的api,发现chrome安全问题,需要解决跨域问题.现给出解决方案: 1.增加proxy.conf.json文件 位置与package.json文件同级(可指定) 2.pac ...