LightOJ 1030 Discovering Gold (概率/期望DP)
题目链接:LightOJ - 1030
Description
You are in a cave, a long cave! The cave can be represented by a \(1 \times 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 \(N^{th}\) 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 \(i^{th}\) integer of this line denotes the amount of gold you will get if you come to the \(i^{th}\) 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
Solution
题意
有 \(N\) 个格子,每个格子有价值为 \(val[i]\) 的金子,初始你在第一个格子。
每次抛一个 \(6\) 面的骰子,抛到的数为 \(X_i\),就往前走 \(X_i\) 个格子,如果超过格子 \(N\),就重新抛,走到格子 \(N\) 就结束。求拿到金子的价值的期望。
思路
可以用 概率 DP 或 期望 DP 解决。
概率DP
求出到达每个格子的概率,然后乘上每个格子的价值再累加起来就行。
设 \(dp[i]\) 为到格子 \(i\) 的概率,则 \(dp[i + j] = dp[i + j] + dp[i] / k\ (1\le j\le k,\ k = min(6, n - i))\)。
期望DP
设 \(dp[i]\) 为格子 \(i\) 到 \(N\) 的能获得黄金的期望,则 \(dp[N] = val[N]\)。
状态转移方程为 \(dp[i] = \frac{1}{k} \sum_{j=1}^kdp[i+j]\ (k = min(6, n - i))\)。
Code
概率DP
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int v[maxn];
double dp[maxn];
int main() {
int T;
scanf("%d", &T);
int kase = 0;
while(T--) {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &v[i]);
}
memset(dp, 0, sizeof(dp));
dp[1] = 1.0;
for(int i = 1; i <= n; ++i) {
int k = min(6, n - i);
for(int j = 1; j <= k; ++j) {
dp[i + j] += dp[i] * 1.0 / k;
}
}
double ans = 0.0;
for(int i = 1; i <= n; ++i) {
ans += dp[i] * v[i];
}
printf("Case %d: %.7lf\n", ++kase, ans);
}
return 0;
}
期望DP
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int v[maxn];
double dp[maxn];
int main() {
int T;
scanf("%d", &T);
int kase = 0;
while(T--) {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &v[i]);
}
memset(dp, 0, sizeof(dp));
dp[n] = v[n];
for(int i = n - 1; i >= 1; --i) {
dp[i] = v[i];
int k = min(6, n - i);
for(int j = 1; j <= k; ++j) {
dp[i] += dp[i + j] * 1.0 / k;
}
}
printf("Case %d: %.7lf\n", ++kase, dp[1]);
}
return 0;
}
LightOJ 1030 Discovering Gold (概率/期望DP)的更多相关文章
- 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(期望)
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 (期望)
https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...
- LightOJ 1030 Discovering Gold 数学期望计算
题目大意:给出长度为n的一条隧道,每个位置都有一定数量的财宝.给你一枚骰子,roll到几点就前进几步,如果即将到达的地方超过了这条隧道长度,就重新roll一次,走到n点结束.求这个过程能收获多少财宝. ...
- LightOJ - 1030 Discovering Gold —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold PDF (English) Statistics For ...
- LightOj:1030-Discovering Gold(期望dp模板)
传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...
- 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)题解
题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望 思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] ...
随机推荐
- WebForm 用户控件 委托 实现 textbox后台赋值 调用端处理实现 textchange
新建一个简单的用户控件,如下图所示 textbox只读,button按钮模拟实现一堆业务逻辑后对textbox赋值. 用户控件后台代码也很简单 public partial class UTTCont ...
- Java并发Lock接口
java.util.concurrent.locks.Lock接口用作线程同步机制,类似于同步块.新的锁定机制更灵活,提供比同步块更多的选项. 锁和同步块之间的主要区别如下: 序列的保证 - 同步块不 ...
- java反射(一)--认识反射机制
一.认识java反射机制 在java语言中,之所以会有如此众多的开源技术支撑,很大的一部分来源于java最大特征--反射机制.能够灵活的去使用反射机制进行项目的开发与设计,才能够真正接触到java的精 ...
- go读写文本文件
一.文件读取 1. 将整个文件读取到内存中 package main import ( "flag" "fmt" "io/ioutil" ) ...
- 【学习总结】Python-3-算术运算符中的/和//
参考:菜鸟教程-Python3运算符 参考:菜鸟教程-Python3数字 算术运算符中的两种除法的区别: 一个斜杠/:正常的人类除法,两个int相除也保留小数 eg: 21/10 = 2.1 两个斜杠 ...
- 错误提示:Wrong Local header signature: 0xE011CFD0
导入Excel时出现错误,错误提示:Wrong Local header signature: 0xE011CFD0,这个是excel的扩展名问题,.xlsx 应该XSSFWorkbook work ...
- IDA技巧
3. 用[shift+F12]调出字符串表,再根据这些字符串,查看引用的地方,也可以分析出一些sub_0XXXX函数的功能. 进入import函数内 x查看调用处 jump to xref 跳到引用 ...
- java switch语句注意事项
/* switch语句的使用注意事项: 1.多个case后面的数据不可以重复 2.switch后面的小括号当中只能是下列数据类型: 基本数据类型:byte . short.char.int 引用数据类 ...
- Qt error: C2236: 意外的标记“class”。是否忘记了“;”?
前阵子玩了一个比较大的程序,手脚,身子脑袋都分开写的那种,因此互相include .h比较多,那么问题来了,有些cpp没有include 的类却使用了起来 ,这时候IDE不会出这个类没有定义什么的,而 ...
- Qt + VS 【如何添加图片资源】
熟悉qt creator,之后发现其debug能力不如vs强,随后转战 qt + vs. 发现图片资源添加不像qt那样直接添加,vs本身会生成一个qrc,我们可以直接去打开然后添加,不必在自己去添加, ...