B. Let's Play Osu!
题目连接:
http://www.codeforces.com/contest/235/problem/B
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.
Input
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
Output
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Sample Input
3
0.5 0.5 0.5
Sample Output
2.750000000000000
Hint
题意
有一个人在玩osu,你有p[i]的概率得到o,有1-p[i]的概率得到x
你的分数等于连续o的长度的平方和
比如ooxoooxoo的分数就是2*2+3*3+2*2 = 17
现在给你p数组,问你期望得分是多少
题解:
n^2 = 2*C(n,2) + n
根据这个来DP就简单了,我们首先对于每一个位置,算出这个位置o的期望长度L就好了
他这个点的贡献就很显然是 2 * L - p[i]
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
double p[maxn];
double ans,tmp;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf",&p[i]);
for(int i=1;i<=n;i++)
{
tmp = tmp * p[i] + p[i];
ans += 2 * tmp;
ans -= p[i];
}
printf("%.12f
",ans);
}