小Biu的骰子(概率dp)

小Biu的骰子(概率dp)

题目描述

从左到右有n个方格,每一块方格上有x[i] 块黄金,最初站在第一块方格上,有一个6个面的均匀骰子,
每一个面上的权值是1−6,每次掷骰子之后按照点数y跳到y步之后的方格,如果超出范围,则重新掷骰子,问到达第n个方格能得到的期望黄金数。

输入

第一行两个整数n(1<=n<=1000)。
第二行n个整数,第i个整数为x[i],(1<=x[i]<=1000)。

输出

输出一个实数表示答案,结果误差范围取10 -6

样例输入 Copy

3
3 6 9

样例输出 Copy

15

提示

样例解释
对于这个样例,第一次仅掷1或2满足题意,可以认为两种情况概率均为50%。
掷1:从黄金数3的格子走到黄金数6的格子,之后只能选择黄金数9的格子,获得黄金数3+6+9=18;
掷2:从黄金数3的格子直接到黄金数9的格子,获得黄金数3+9=12;
因此获得黄金数期望=12*50%+18*50%=15

对于10%的数据,1<=n<=10
对于50%的数据,1<=n<=100
对于100%的数据,1<=n<=1000

思路:dp[i]表示从i到n的期望。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
 
#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define per(i , a , b) for(register int i=(a);i>=(b);i--)
#define ms(s) memset(s, 0, sizeof(s))
#define squ(x) (x)*(x)
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll , ll> pi;
typedef unordered_map<int,int> un_map;
typedef priority_queue<int> prque;
 
template<class T>
inline void read (T &x) {
    x = 0;
    int sign = 1;
    char c = getchar ();
    while (c < '0' || c > '9') {
        if ( c == '-' ) sign = - 1;
        c = getchar ();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar ();
    }
    x = x * sign;
}
 
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const ll INF = ll(1e18);
const int mod = 1e9+7;
const double PI = acos(-1);
//#define LOCAL
 
int n;
double x[maxn];
 
double dp[maxn];
int main(int argc, char * argv[])
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    read(n);
    rep(i,1,n) {
        scanf("%lf",&x[i]);
        dp[i]=x[i];
    }
    dp[1]=x[1];
    per(i,n-1,1) {
        int cnt = 0;
        double t=0.0;
        rep(j,1,6) {
            if(i+j<=n) {
                cnt++;
                t+=dp[i+j];
            }
        }
        dp[i]+=t/cnt;
    }
    printf("%.6f\n",dp[1]);
    return 0;
}
发布了259 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/dy416524/article/details/105733969
biu