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

Posted Zars19

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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\n",f[n]);
    }
    return 0;
} 

 

以上是关于[Codeforces Round #146 (Div. 1) B]Let's Play Osu!(期望Dp)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #797 (Div. 3) D, E, F, G题题解

Codeforces Round #402 (Div. 2) D

Codeforces Round #408 (Div. 2) D

Codeforces Round #390 (Div. 2) D

Codeforces 671B/Round #352(div.2) D.Robin Hood 二分

Educational Codeforces Round 18 D