LightOJ 1030 数学期望
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
做法:那么这个点的期望dp[i] = dp[i +1] /6 + dp[i + 2] / 6 + dp[i + 3] /6 + dp[i + 4] / 6 + dp[i + 5] / 6 + dp[i + 6] / 6 + dp[i];
对于小于6的点另做处理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm> using namespace std;
typedef long long LL;
#define N 210
#define INF 0x3f3f3f3f double a[N], dp[N]; int main()
{
int T, n, cas=1;
scanf("%d", &T); while(T--)
{
scanf("%d", &n);
memset(dp, 0, sizeof(dp));
for(int i=0; i<n; i++)
scanf("%lf", &a[i]); dp[n-1]=a[n-1];
for(int i=n-2; i>=0; i--)
{
dp[i]=a[i];
int t=6;
if(n-1-i<6)
t=n-1-i;
for(int j=1; j<=t; j++)
dp[i]+=dp[i+j]/t;
}
printf("Case %d: %.10f\n", cas++, dp[0]);
}
return 0 ;
}
LightOJ 1030 数学期望的更多相关文章
- LightOJ - 1027 数学期望
题意:有n扇门,每扇门有一个值x,大于0代表x分钟后出去,小于0代表x分钟后回到原地,求出去的时间的期望 题解:假设出去的总时间为sum1,回来的总时间为sum2,出去的门个数为out,进来的门的个数 ...
- (LightOJ 1030)期望dp
You are x N grid. Each cell of the cave can contain any amount of gold. Initially you are . Now each ...
- 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)
题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...
- [BZOJ 3143][HNOI2013]游走(数学期望)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3143 分析: 易得如果知道了每条边经过的数学期望,那就可以贪心着按每条边的期望的大小赋 ...
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)
题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是 ...
- 数学期望和概率DP题目泛做(为了对应AD的课件)
题1: Uva 1636 Headshot 题目大意: 给出一个000111序列,注意实际上是环状的.问是0出现的概率大,还是当前是0,下一个还是0的概率大. 问题比较简单,注意比较大小: A/C & ...
- [2013山东ACM]省赛 The number of steps (可能DP,数学期望)
The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
- 【BZOJ2134】单位错选(数学期望,动态规划)
[BZOJ2134]单位错选(数学期望,动态规划) 题面 BZOJ 题解 单独考虑相邻的两道题目的概率就好了 没了呀.. #include<iostream> #include<cs ...
- 【BZOJ1415】【NOI2005】聪聪和可可(动态规划,数学期望)
[BZOJ1415][NOI2005]聪聪和可可(动态规划,数学期望) 题面 BZOJ 题解 先预处理出当可可在某个点,聪聪在某个点时 聪聪会往哪里走 然后记忆化搜索一下就好了 #include< ...
随机推荐
- MySQL日期时间加|减法
日期加法 select date_add(curdate(), interval N SECOND); -- 加N秒 select date_add(curdate(), interval N MIN ...
- Linux云服务器购买,学习
购买云服务器的初衷 作为一名自动化测试工程师,不能仅限于掌握工作上的业务和代码,业余时间需要找点开源项目来练习性能.接口.UI自动化. 云服务器购买 https://www.aliyun.com/ 我 ...
- Win11环境Mecab日语分词和词性分析以及动态库DLL not found问题(Python3.10)
日语因为存在假名,会导致翻译软件进行翻译时机翻味道过重的问题,比如積ん読(つんどく)这个词,大多数软件会翻译成:堆积的读,但其实是明明买了书却不读,光放着的意思.有时候也需要单独查句子中的单词释义来理 ...
- 【scikit-learn基础】--『监督学习』之 逻辑回归分类
逻辑回归这个算法的名称有一定的误导性.虽然它的名称中有"回归",当它在机器学习中不是回归算法,而是分类算法.因为采用了与回归类似的思想来解决分类问题,所以它的名称才会是逻辑回归. ...
- electron入门之试调工具remote(三)
electron入门到入土,从渲染线程中创建新窗口.2022-03-21入门版本17.1.2 electron重要概念,只有一个主线程,其他都是渲染进程或者叫子线程,他们不能直接相互操作,可以通过ip ...
- Java 并发编程(六)并发容器和框架
传统 Map 的局限性 HashMap JDK 1.7 的 HashMap JDK 1.7 中 HashMap 的实现只是单纯的 "数组 + 链表 " 的组合方式,具体的组成如下: ...
- 谈谈muduo库的销毁连接对象——C++程序内存管理和线程安全的极致体现
前言 网络编程的连接断开一向比连接建立复杂的多,这一点在陈硕写的muduo库中体现的淋漓尽致,同时也充分体现了C++程序在对象生命周期管理上的复杂性,稍有不慎,满盘皆输. 为了纪念自己啃下muduo库 ...
- spring-mvc 系列:域对象共享数据
目录 一.使用ServletAPI向request域对象共享数据 二.使用ModelAndView向request域对象共享数据 三.使用Model向request域对象共享数据 四.使用Map向re ...
- GaussDB(for Redis)游戏实践:玩家下线行为上报
本文分享自华为云社区<GaussDB(for Redis) 游戏实践:玩家下线行为上报>,作者:GaussDB 数据库 为保护未成年人的身心健康,2007年国家推出网络游戏防沉迷系统,对未 ...
- Tarjan:这个算法大神
摘要:图的算法是进行静态分析的基础数据算法,如何提高图的分析效率,就需要对图的算法有进一步的认识. 1. 引言 在静态分析技术中, 我们常用会将代码转成抽象语法树(AST), 然后采用深度遍历(DFS ...