BZOJ-3450 Easy probability dp "mathematical expectation"

Source: Internet
Author: User

Description

One day WJMZBMR is playing osu~~~ but he is too weak to push, some places entirely by luck: (
Let's simplify the rules of the game.
Have n click to do, success is O, failure is x, score is calculated according to comb, continuous a comb there is a*a points, comb is a great continuous o.
Like Ooxxxxooooxxx, the score is 2*2+4*4=4+16=20.
Sevenkplus Idle panic To see he played a plate, some places have nothing to do with luck either O or x, some places o or x each have 50% probability, with the number to express.
For example, OO?XX is a possible input.
So what is the expected score of WJMZBMR this OSU?
Like Oo?xx's words, "o" is the word Oooxx = 9, x is ooxxx = 4
The natural expectation is (4+9)/2 = 6.5.

Input


The first line is an integer n, which indicates the number of clicks
Next string, each character is an ox? In one of

Output

A single line of floating-point numbers indicates the answer
Rounded to 4 digits after the decimal point
If the fear of precision kneeling suggested with a long double or extended

Sample Input4
????
Sample Output4.1250

n<=300000
Solvingtaking into account each contribution to the expectation, it is assumed that in the processing of the position I, I have a continuous o, which can be considered a special case.    Suppose this bit is o then? i = (l + 1) ^ 2-l ^ 2 = 2 * l + 1, while taking expected E (? i) = 2 * E (L) + 1;Suppose this bit is x then? i = 0;
Suppose this one is? Then E (? i) = P1 * X1 + p0 * X0 (where P1 is the probability of taking one of the bits, P0 is the probability of taking 0, X1 for this bit to take 1 of the score changes, X0 the same), then X0 = 0, so e (? i) = P1 * X1,e (? i) = 0.5 * (2 * E (L) + 1) = e (L) + 0.5. then the question becomes how to solve the desired length. Suppose this bit is O then E (l) + 1;Suppose this bit is x so E (L) = 0;Suppose this bit is? Then E (l) = 0.5 * 0 + 0.5 * (E (L) + 1);  O (n) can be processed.
1#include <bits/stdc++.h>2 #defineRep (I, A, b) for (int i = A; I <= b; i++)3 #defineDrep (I, A, b) for (int i = A; I >= b; i--)4 #defineREP (I, A, b) for (int i = A; I < b; i++)5 #definePB Push_back6 #defineMP Make_pair7 #defineCLR (x) memset (x, 0, sizeof (x))8 #defineXX First9 #defineyy secondTen using namespacestd; OnetypedefLong Longi64; Atypedef pair<int,int>PII; - Const intINF = ~0U>>1; - Consti64 INF = ~0ull >>1; the //*************************************** -   - intMain () { -     intN; +scanf"%d\n", &n); -     DoubleL0), ans (0); +     Charch; A      while(n--) { atCH =GetChar (); -         if(ch = ='o') ans + =2* L +1, l++; -         Else if(ch = ='x') L =0; -         ElseAns + = L +0.5, L = (l +1) /2; -     } -printf"%.4LF", ans); in     return 0; -}
View Code

BZOJ-3450 Easy probability dp "mathematical expectation"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.