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 ...
随机推荐
- DBA操作
sqlplus sys/tiger as sysdba; alter user scott account unlock; 用户已更改 切换用户:conn scott/tiger as sysdba ...
- logback -- 配置详解 -- 一 -- <configuration>及子节点
附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...
- RF实现多次失败重跑结果合并的基础方法和优化方法
实现思路:通过分次执行失败案例重跑,然后通过结果文件合并命令实现多次失败重跑结果文件的合并,并输出合并后的log和report文件: 说明:具体失败案例重跑命令和结果文件合并命令请参考本博客其他相关章 ...
- java jni调用
http://www.cnblogs.com/mandroid/archive/2011/06/15/2081093.html
- Steam安装Google Earth VR
打开Steam 打开火狐浏览器 输入steam://install/348250
- CentoOS6.6安装netcat
http://blog.csdn.net/u013673976/article/details/47084841 CentOS下安装netcat 使用zookeeper过程中,需要监控集群状态.在使用 ...
- 使用es6使数组的第一项和最后一项就行调换
let arr = [1, 2, 3,4]; let a = arr.filter((item, index) => { return index > 0 && index ...
- 《转载》Python并发编程之线程池/进程池--concurrent.futures模块
本文转载自Python并发编程之线程池/进程池--concurrent.futures模块 一.关于concurrent.futures模块 Python标准库为我们提供了threading和mult ...
- java框架---->quartz的使用(一)
Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.今天我们就来学习一下它的使用,这里会分篇章对它进行介绍.只是希望能有个人,在我说没事的时候,知道我不 ...
- Qt获取CPU编号和硬盘序列号
windows下执行命令除了用cmd之外,还有个东西叫WMIC,非常强大,可以通过他获取很多信息,包括硬件信息. QString frmMain::getWMIC(const QString & ...