zoukankan      html  css  js  c++  java
  • [LOJ 1030] Discovering Gold

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

    概率DP:一般求概率是正推,求期望是逆推。
    设(dp[i])表示当前位置在(i)处到达(N)处得到的金币期望,
    (dp[i]=SUM(dp[i+1],dp[i+2]..dp[i+6])/6+a[i]);
    当(N-i<6)时,注意特殊处理。

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    #define N 110
    
    int main()
    {
        int T,iCase=1;
        int n,a[N];
        double dp[N];
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=n;i++) scanf("%d",&a[i]);
            for(int i=n;i>=1;i--)
            {
                dp[i]=a[i];
                double t=0;
                int d=min(6,n-i);
                if(d<=0) continue;
                for(int j=1;j<=d;j++)
                {
                    t+=dp[i+j];
                }
                dp[i]+=t/d;
            }
            printf("Case %d: ",iCase++);
            printf("%.10f
    ",dp[1]);
        }
        return 0;
    }
  • 相关阅读:
    lua编程之协程介绍
    lua编程之元表与元方法
    设计模式系列之单例模式
    设计模式系列之生成器模式
    设计模式系列之抽象工厂模式
    设计模式系列之原型模式
    设计模式系列之工厂模式
    stl源码分析之hash table
    2018/2019款 MacBookPro 接口失灵的原因及解决方案
    test
  • 原文地址:https://www.cnblogs.com/hate13/p/4552818.html
Copyright © 2011-2022 走看看