[Codeforces Round #146 (Div. 1) B]Let's Play Osu!(期望Dp)

Description

You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".

Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.

You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.

Solution

题意:n个位置,每个位置有pi的概率点击成功,连续的x次成功将会被计分为x2,问总得分的期望

用f[i]表示到第i个位置的期望得分,可以维护一个g[i]表示以i结尾的期望连续成功次数

g[i]=(g[i-1]+1)*pi

f[i]=f[i-1]*(1-pi)+(f[i-1]-g[i-1]2+(g[i-1]+1)2)*pi

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define MAXN 100005
using namespace std;
int n;
double p,f[MAXN],g[MAXN];
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lf",&p);
            g[i]=(g[i-1]+1)*p;
            f[i]=f[i-1]*(1-p);
            f[i]+=(f[i-1]-g[i-1]*g[i-1]+(1+g[i-1])*(1+g[i-1]))*p;
        }
        printf("%.10lf
",f[n]);
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/Zars19/p/6917987.html