Can someone please explain the logic used in the solution of this problem?. I see that they are trying to make a dp[] and then when we query for an input we get the result from the []. But how is this dp[] being developed?.
Please help. Thank you.
# | User | Rating |
---|---|---|
1 | ecnerwala | 3649 |
2 | Benq | 3581 |
3 | orzdevinwang | 3570 |
4 | Geothermal | 3569 |
4 | cnnfls_csy | 3569 |
6 | tourist | 3565 |
7 | maroonrk | 3531 |
8 | Radewoosh | 3521 |
9 | Um_nik | 3482 |
10 | jiangly | 3468 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 174 |
2 | awoo | 164 |
3 | adamant | 161 |
4 | TheScrasse | 159 |
5 | nor | 158 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 152 |
8 | SecondThread | 147 |
9 | orz | 146 |
10 | pajenegod | 145 |
Can someone please explain the logic used in the solution of this problem?. I see that they are trying to make a dp[] and then when we query for an input we get the result from the []. But how is this dp[] being developed?.
Please help. Thank you.
Name |
---|
This is easy version of dp problems : 'cause a,b are only in range [1, 10^5], it means that we can precompute the value of dp[i]: the number of ways that Marmot could eat i flowers following the given rule.
Suppose we got a valid sequence of flowers, then all white flowers must be grouped into some(possibly zero )subsequences of k-consecutive flowers, e.g : k =2 then valid sequences be like : WW, RR, WWR, RRR, WWRR,... invalid seqs including WWW, RWWW,... So, if the end of a valid sequence is 'W' => it must have k consecutive 'W' at the end of the sequence. Since a valid seq of length i could be ended with either 'W' or 'R' -> recursive formula : dp[i] = dp[i-1] + dp[i-k].
We calculate dp from 0 -> 10^5. Then the answer of each query [a, b] could be answer via the consecutive sum dp[a->b].