Light oj 1030 概率DP
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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
Sample Output
Case 1: 101.0000000000
Case 2: 13.000
Case 3: 15
题目大意:抛色子移动,每个地点都有一些金子,问你到到达终点的时候拿到的金子数量的数学期望。
思路分析:刚开始始终都不懂题意,后来百度了一下才知道是让去求期望,但是依然没有什么好的思路,
后来认真的去复习了一些有关数学期望的姿势,知道读到这一句,解决这类问题,对随机变量A、B,有
数学期望E(aA+bB)=aE(A)+bE(b);根据这个不正可以构建状态转移方程用DP做么,每一点的期望可以
由它之前的位置的点的期望求出,DP[i]=a[i]+1/step*dp[i-(1...step)]
但是我现在不太理解的是为什么一定要逆推而不能正推orz,求指教,想明白了以后我也会回来补上。
代码:
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=110;
double dp[maxn],a[maxn];
int kase=0;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf",&a[i]);
memset(dp,0,sizeof(dp));
dp[n]=a[n];
for(int i=n-1;i>=1;i--)
{
dp[i]=a[i];
//cout<<dp[i]<<endl;
int step=min(6,n-i);
for(int j=1;j<=step;j++)
{
dp[i]+=1.0/(step*1.0)*dp[i+j];
}
}
printf("Case %d: %.6lf\n",++kase,dp[1]);
}
return 0;
}
Light oj 1030 概率DP的更多相关文章
- 概率DP light oj 1030
t组数据 n块黄金 到这里就捡起来 出发点1 到n结束 点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6 如果满6个的话 否则 ...
- Light OJ 1030 - Discovering Gold
题目大意: 给你一个1*N的方格,你初始位置是在1,给你一个骰子,假设你现在的位置是X,你投掷一个骰子掷的点数是y, 那么你的新位置就是 X+y, 并且你可以得到新位置的宝藏.假如X+y > N ...
- Light OJ 1030 - Discovering Gold(概率dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...
- loj 1030概率dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 思路:一直以来对这种概率题都挺感冒的=.=......还是说一下思路吧,dp[i ...
- lightoj 1030 概率dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 #include<cstdio> #include<cstri ...
- 玲珑学院oj 1152 概率dp
1152 - Expected value of the expression Time Limit:2s Memory Limit:128MByte Submissions:128Solved:63 ...
- light oj 1205(数位DP)
题目描述: 求给定区间中的回文数有多少个? 首先明确一点,如果一个数是回文数,那么给这个数两边加上相同的数,那么这个数还是回文数. 根据这点就可以进行递推了,p[start][end]=9*p[sta ...
- light oj 1032(数位DP)
求一段区间中,每个十进制数所对应的二进制数中连续的1的个数之和. 设dp[i][0]代表长度为i的二进制数,首位为0,所含有的连续的1的个数之和. dp[i][1]代表长度为i的二进制数,首位为1,所 ...
- light oj 1422 区间dp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
随机推荐
- SQL server 2008无法修改表
长久未使用SQL server,一直都是使用Navicat来处理各种数据库,感觉使用很方便,但由于实际需要,必须要用SQL server创建新的数据库,却意外的遇到了以前从未遇到过的问题. 在建好表以 ...
- Web项目中JSP页面的一种调试方法与出现的问题 -- SpringMVC架构测试
在前端开发中,尤其是MVC架构多人开发,负责前端的童鞋总是需要做静态页面,再和后台连接前无法使用变量如EL表达式等测试功能,所以本人引入了一个模板jsp数据测试专用文件,专门配置所有的变量,然后在待测 ...
- POJ3253 Haffman
POJ3253 分析: 简单的哈弗曼树的应用. AC代码: //Memory: 316K Time: 16MS #include <iostream> #include <cstri ...
- dedecms文章的更新时间问题 每次更改文章时间变成最新的
dedecms 每次更改文章,更新时间这里每次改了后再来看又变成当前最新时间的了. 解决方法: 查找后台目录的 templets/article_edit.htm 这个文件. 然后打开,查找如下代码: ...
- ZooKeeper笔记--集群安装配置 【转】
ZooKeeper是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(Distributed Synchronization).命名服务(Naming S ...
- opencv 批量图像读写
处理图像数据集时通常要读写整个文件夹里的图像,这时就会用的图像的批量读写. 比较常用的方法就是生成一个包含所有图像的txt列表 生成txt文件的方法如下: 利用cmd进入dos 利用路径进入指定文件夹 ...
- TestNG使用Eclipse建立Test Case - 就是爱Java
除了JUnit可以进行单元测试外,还可以使用TestNG来撰写Test Case,这是另一种测试Framework,它是为更广泛的测试场合而设计,可以运行在没有修改过的JUnit测试,除非看到它们的i ...
- CCI_chapter 13C++
13.9Write a smart pointer (smart_ptr) class template<class T>class SmartPoint{ public: SmartPo ...
- VC 隐藏托盘图标
苦苦寻找的隐藏托盘图标的方法,今天终于搞定,献给大家! #include <atlbase.h> #include <atlconv.h> #include <CommC ...
- 如何让 Qt 的程序使用 Sleep(主线程没有Sleep函数,但线程可用自带的保护函数Sleep)
熟悉的陌生人 Qt 是事件驱动的,所以当你用Qt的时候,几乎时时刻刻和 QEventLoop 打交道.,只是你可能没有意识到: QCoreApplicaton::exec() QApplication ...