Lightoj 1030 - Discovering Gold

题目大意:一个人走n个格子到终点。通过骰子确定每次走几步。每个格子上有黄金,问最后得到黄金数量的期望。

假设dp[i]为到第i个格子的概率。

a[i]为第i个格子的黄金数量。

那么期望就是  Σa[i]*dp[i]

重点是怎么求概率。

拿样例举例。

3

3 6 9

dp[1]=1;没毛病

dp[2]=0.5

dp[3]=dp[1]*0.5+dp[2]*1=1

/* ***********************************************
Author        :guanjun
Created Time  :2016/7/9 23:17:57
File Name     :1030.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
int n;
double a[maxn];
double dp[maxn];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int T;
    cin>>T;
    for(int t=1;t<=T;t++){
        cin>>n;
        for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
        cle(dp);
        double ans=0;
        dp[1]=1;
        for(int i=1;i<=n;i++){
            for(int j=max(1,i-6);j<i;j++){
                int k=min(n-j,6);
                dp[i]+=dp[j]/(k*1.0);
            }
            ans+=dp[i]*a[i];
        }
        printf("Case %d: %.10f\n",t,ans);
    }
    return 0;
}
时间: 2024-08-03 07:05:34

Lightoj 1030 - Discovering Gold的相关文章

LightOJ 1030 Discovering Gold (概率/期望DP)

题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 \times 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\) s

lightoj 1030 Discovering Gold[ 期望 ]

B - Discovering Gold 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 yo

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 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 a

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>

LightOJ 1030 Discovering Gold(期望 概率)

正推,到达i的概率为p[i],要注意除了1和n外,到达i的概率并不一定为1 概率表达式为p[i] += p[j] / min(n - j, 6) 从j带过来的期望为exp[i] += exp[j] / min(n - j, 6) 又到达i时有价值val[i],到达i的概率为p[i],故exp[i] += val[i] * p[i] #include<cstdio> #include<iostream> #include<cstdlib> #include<cstr

LightOJ 1030 Discovering Gold 数学期望计算

题目大意:给出长度为n的一条隧道,每个位置都有一定数量的财宝.给你一枚骰子,roll到几点就前进几步,如果即将到达的地方超过了这条隧道长度,就重新roll一次,走到n点结束.求这个过程能收获多少财宝. 题目思路:很明显问题是求期望值的. 期望值公式: E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) (p为概率,x为某一点价值). 具体看代码 #include<cstdio> #include<stdio.h> #include<cstdl

LightOJ 1030 Discovering Gold (期望)

https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次摇骰子,他就前进几步,但有一种情况例外,如果当前位置+色子数 > N,那么他就会重新摇色子. 走到N这个位置的话,意味着游戏结束了. 问游戏结束时,这个人得到金币的期望. 思路:这里给出两种做法,一种是正序求解,一种是逆序求解. ①正序求解: 这种做法是从前往后计算每个格子的概率,假设我们现在处于第

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 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位置的能够