Light oj 1030 概率DP

D - Discovering Gold

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status Practice 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

题目大意:抛色子移动,每个地点都有一些金子,问你到到达终点的时候拿到的金子数量的数学期望。

思路分析:刚开始始终都不懂题意,后来百度了一下才知道是让去求期望,但是依然没有什么好的思路,

后来认真的去复习了一些有关数学期望的姿势,知道读到这一句,解决这类问题,对随机变量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;
}

时间: 2024-12-14 04:27:35

Light oj 1030 概率DP的相关文章

Light OJ 1030 - Discovering Gold(概率dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的地方, 每个格子中都有价值为v[i]的宝藏. 有一个6面的骰子,数字为从1-6, 每次摇一次骰子, 得到的数字x后, 你可以到达距离当前位置大x的位置, 并且得到那个位置的宝藏. 如果要走的位置在n的外面, 那么在此摇骰子, 直到找到一个合适的数字.到达n位置的时候结束. 现在想知道走到n位置的能够

[水+期望dp] light oj 1030 Discovering Gold

题意: 给n个点,每个点都有一个财宝. 你从1这个点开始出发,假设你在i这个点,每次随机走1~min(6,n-i)步. 每到达一个点就拿走财宝. 问最后拿到财宝的期望. 思路: 水的题目. dp[n]=v[n] 然后逐个往前推. 就是注意一下步数是 1~min(6,n-i) 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #inc

Light OJ 1030 - Discovering Gold(期望)

1030 - Discovering Gold PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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

light oj 1084 线性dp

1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <queue> 6 #define ll long long 7 8 using namespace std; 9 const int N = 1e5+1000; 10 11 int a[N],dp[N]; 12 13 void solve() 14 {

light oj 1422 区间dp

1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream> 6 #include <algorithm> 7 #include <climits> 8 #include <queue> 9 #define ll long long 10 11 using na

light oj 1068 数位dp

1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream> 6 #include <algorithm> 7 #include <climits> 8 #include <queue> 9 #define ll long long 10 11 using na

light oj 1031(数位DP)

求一段区间中,每个十进制数所对应的二进制数中连续的1的个数之和. 设dp[i][0]代表长度为i的二进制数,首位为0,所含有的连续的1的个数之和. dp[i][1]代表长度为i的二进制数,首位为1,所含有的连续的1的个数之和. a: d[i][1]=d[i-1][0]+d[i-1][1]+(1<<(i-2)); b: d[i][0]=d[i-1][0]+d[i-1][1]; 这里面有一个需要注意的地方是,假设有一个数字是111,那么它含有2个连续的1,具体体现在 方程上是分两次计算的,一个是a

Lightoj 1030 概率dp

Problem: Analyse: dp[i]为i开始走到结尾的价值, 那么dp[i]是从后面的6个转移过来的. 这样我们就倒着递推就好了(后面的要先算好). 注意后面不足六个的时候的处理情况. /**********************jibancanyang************************** *Author* :jibancanyang *Created Time* : 一 5/ 9 20:38:59 2016 *File Name* : .cpp **Code**:

Light oj 1030 二分查找

1088 - Points in Segments   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a seg