【LightOJ - 1030】Discovering Gold(概率dp,数学期望,期望的线性性)

题干:

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

题目大意:

在一个1*N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币。 
现在有一个人在1这个位置,手里有一颗色子,色子摇到几,他就前进几步,但有一种情况例外,如果当前位置+色子数 > N,那么他就会重新摇色子。 
走到N位置,意味着游戏结束了。 
问游戏结束时,这个人得到金币的期望。

解题报告:

首先根据期望的线性性转化题意:考虑每个点被选中的概率,走到每个点的概率乘以该点的权值 的和。

dp[i]表示走到i这个点的概率是多少。“我为人人”法转移。(最后的dp[n]肯定==1)

最后乘以权值就是答案。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,a[MAX];
double dp[MAX];
int main()
{int t,iCase=0;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) scanf("%d",a+i);printf("Case %d: ",++iCase);for(int i = 1; i<=n+6; i++) dp[i]=0;dp[1]=1;for(int i = 1; i<=n; i++) {
//			for(int j = 1; j<=6; j++) {
//				if(i-j >= 1) dp[i] += dp[i-j];
//			}int up = min(n-i,6); for(int j = 1; j<=up; j++) dp[i+j] += dp[i]/up;}double ans = 0;for(int i = 1; i<=n; i++) ans += dp[i] * a[i];printf("%.8f\n",ans);}return 0 ;
}

思路2:(链接)

这题要倒着推,由N推向1 
设dp[k]为到达k这个位置时得到金币的期望,m为该点和N这个位置的距离,gold[k]为k这个位置的金币数,因为走的位置不能超过N,所以要取min(m,6) 
那么dp[k] = 1 / min(m,6) * (dp[k + 1] + dp[k+2] + … + dp[min(m,6)]) + gold[k]。

(其实是因为这题巧了,所以dp[k]可以等概率的由后面的点转移而来,但是第一种方法更具有普遍性)

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
using namespace std;
const int N = 110;
int gold[N];
double dp[N];
int n;double solve() {for(int i = 0; i < n - 1; i++) dp[i] = 0;int step;dp[n-1] = gold[n-1];for(int i = n - 2; i >= 0; i--) {dp[i] = gold[i];step = min(6, n-1-i);for(int j = 1; j <= step; j++)dp[i] += 1.0 / step * dp[i+j];}return dp[0];
}int main() {int test, cas = 1;scanf("%d", &test);while(test--) {scanf("%d", &n);for(int i = 0; i < n; i++)scanf("%d", &gold[i]);printf("Case %d: %.7lf\n", cas++, solve());}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440056.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【LightOJ - 1079】Just another Robbery(概率dp,概率背包)

题干&#xff1a; As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermi…

强烈推荐的TensorFlow、Pytorch和Keras的样例资源(深度学习初学者必须收藏)

目录 一、TensorFlow 二、Keras 三、Pytorch 总结 本文转自微信公众号&#xff1a;机器学习初学者 原创&#xff1a; 机器学习初学者 机器学习初学者 TensorFlow、Keras和Pytorch是目前深度学习的主要框架&#xff0c;也是入门深度学习必须掌握的三大框架&#xff0c;但…

由浅到深理解ROS(1)

ROS机器人操作系统 &#xff08; Robot Operating System 或简称 ROS&#xff09;&#xff0c;可以帮助提高机器人软件的开发效率。ROS能够提供类似传统操作系统的诸多功能&#xff0c;如硬件抽象、底层设备控制、常用功能实现、进程间消息传递和程序包管理等。此外&#xff0c…

Centos7换yum源

1.进入到yum的源目录下 cd /etc/yum.repos.d/2.将原来的CentOS-Base.repo进行备份 mv CentOS-Base.repo CentOS-Base.repo_back3.下载阿里源 http://mirrors.aliyun.com/repo/Centos-7.repo 到/etc/yum.repos.d/目录下 4.改变名字 mv Centos-7.repo CentOS-Base.repo5.生成…

(3)链表

目录 下面是关于链表的一些知识点&#xff1a; 链表的操作&#xff1a; 创建链表 遍历链表 判断链表是否为空 求链表长度 插入元素 删除元素 排序链表 这篇笔记是根据郝斌老师的上课讲义整理而得&#xff1a; 前面介绍了连续的存储结构&#xff1a;数组&#xff0c;今…

【LightOJ - 1027】A Dangerous Maze(概率dp,数学期望)

题干&#xff1a; You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors. If you choose the ith door, it can either take you back to the same position wh…

由浅到深理解ROS(2)

ROS文件系统 用户可以直接参看官网&#xff1a; http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念&#xff1a;Package和Manifest&#xff0c;即包和清单文件。 &#xff08;1&#xff09;Package是组织ROS代码的最基本单位&…

Django员工管理系统

Django员工管理系统&#xff08;ems&#xff09; 需求分析&#xff1a; 1.实现管理员的注册、登陆页面注册&#xff1a;用户名、真实名字、密码、确认密码、性别、验证码用户名需要判断是否合法、是否存在&#xff0c;loading图片提示密码和确认密码是否相同&#xff0c;load…

(4)栈

目录 栈的知识点&#xff1a; 栈的操作&#xff1a; 创建栈&#xff1a; 压栈&#xff1a; 遍历栈&#xff1a; 判断栈是否为空&#xff1a; 出栈 清空栈 这篇笔记是根据郝斌老师的上课讲义整理而得。 栈的知识点&#xff1a; 栈的定义&#xff1a;一种可以实现"…

【LightOJ - 1104】Birthday Paradox(概率,思维)

题干&#xff1a; Sometimes some mathematical results are hard to believe. One of the common problems is the birthday paradox. Suppose you are in a party where there are 23 people including you. What is the probability that at least two people in the party…

爬虫小记

中国商标网 找到正确的入口 在此页面加入全部data数据获取xhr请求包

由浅到深理解ROS(3)-命名空间

全局命名空间&#xff1a; /rosout前面的反斜杠“/”表明该节点名称属于全局命名空间。之所以叫做全局名称因为它们在任何地方&#xff08;包括代码、命令行工具、图形界面工具等的任何地方&#xff09;都可以使用。无论这些名称用作众多命令行工具的参数还是用在节点内部&…

(5)队列

目录 队列知识点&#xff1a; 循环队列&#xff1a; 队列的操作&#xff1a; 创建队列&#xff1a; 判断队列是否已满&#xff1a; 入队&#xff1a; 遍历队列&#xff1a; 判断队列是否为空&#xff1a; 出队&#xff1a; 这篇笔记是根据郝斌老师的上课讲义整理而得&…

【LightOJ - 1038】Race to 1 Again(概率dp,数学期望)

题干&#xff1a; Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. So, he is now playing with this property. He selects a number N. And he calls this D. In each turn he randomly choose…

使用matplotlib进行简单的数据展示

import numpy as np import matplotlib.pyplot as plt# 解决中文乱码 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签 plt.rcParams[axes.unicode_minus] False # 用来正常显示负号# 建立一个坐标系 plt.subplot(1, 1, 1) # 指明x和y值 x np.array([1, 2…

(6)递归

目录 递归知识点&#xff1a; 递归操作&#xff1a; 阶乘的循环实现&#xff1a; 阶乘的递归实现&#xff1a; 经典的汉诺塔问题&#xff1a; 这篇笔记是根据郝斌老师的上课讲义整理而得。 递归知识点&#xff1a; 定义&#xff1a;一个函数自己直接或间接调用自己 。 …

由浅到深理解ROS(4)

消息和消息类型 节点能相互传递消息&#xff0c;节点之间通信也是基于消息。消息类型也就是数据类型数据类型&#xff0c;理解消息的类型很重要&#xff0c;因为它决定了消息的内容。也就是说&#xff0c;一个话题的消息类型能告诉你该话题中每个消息携带了哪些信息&#xff0…

【CodeForces - 1197C】Array Splitting(水题)

题干&#xff1a; You are given a sorted array a1,a2,…,ana1,a2,…,an (for each index i>1i>1 condition ai≥ai−1ai≥ai−1holds) and an integer kk. You are asked to divide this array into kk non-empty consecutive subarrays. Every element in the array…

Django项目部署在Centos7

把Django项目部署在Centos7下 先有一个Django项目 一个员工管理系统&#xff08;ems&#xff09; 员工管理系统 使用VMware创建一个虚拟的Linux系统 Centos7下安装MySql5.7 详细安装 Centos7下安装Python3.5 详细安装 Centos7下安装Django2.0.6 - 安装数据库驱动&#xf…

(7)树

目录 树的知识点&#xff1a; 定义&#xff1a; 专业术语&#xff1a; 操作(重点): 树的操作&#xff1a; 创建二叉树&#xff1a; 先序遍历&#xff1a; 中序遍历&#xff1a; 后序遍历&#xff1a; 这篇笔记是根据郝斌老师的上课讲义整理而得。 树的知识点&#xf…