BZOJ 4318: OSU! [Expect DP]

4318: OSU!

Time Limit: 2 Sec Memory Limit: 128 MB

Description

osu is a casual software that the masses love to see.
We can simplify and adapt the rules of osu ​​as follows:
there are a total of n operations, each operation is only divided into success and failure, success corresponds to 1, failure corresponds to 0, n operations corresponds to 1 01 of length n string. Consecutive X 1s in this string can contribute a fraction of X^3, and these x 1s cannot be contained by other consecutive 1s (that is, a very long string of 1s, see the example explanation for details).
Now give n, As well as the success rate of each operation, please output the expected score, and the output is rounded to 1 decimal place.

Input

The first line has a positive integer n, which represents the number of operations. The next n lines each have a real number between [0, 1], indicating the success rate of each operation.

Output

There is only one real number, representing the answer. Answers are rounded to 1 decimal place.

Sample Input

3
0.5
0.5
0.5

Sample Output

6.0

HINT

[Example description]
The 000 score is 0, the 001 score is 1, the 010 score is 1, the 100 score is 1, the 101 score is 2, the 110 score is 8, the 011 score is 8, the 111 score is 27, and the sum is 48. Expected is 48/8=6.0
N<=100000

answer

If the current number is 0, then no new increment will be born, but if the current number is 1, then the new increment is (X+1)^3-X^3, and the simplification is (3X^2+ 3X+1). Then you only need to maintain X^2 and X.

code show as below

#include<cstdio>
using namespace std;
int n;
double f[100005],X2[100005],X1[100005];
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        double Now;
        scanf("%lf",&Now);
        X2[i]=(X2[i-1]+2*X1[i-1]+1)*Now;
        X1[i]=(X1[i-1]+1)*Now;
        f[i]=f[i-1]+(3*X2[i-1]+3*X1[i-1]+1)*Now;
    }
    printf("%.1lf\n",f[n]);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325648365&siteId=291194637