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.
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.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3690 |
2 | jiangly | 3647 |
3 | Benq | 3581 |
4 | orzdevinwang | 3570 |
5 | Geothermal | 3569 |
5 | cnnfls_csy | 3569 |
7 | Radewoosh | 3509 |
8 | ecnerwala | 3486 |
9 | jqdai0815 | 3474 |
10 | gyh20 | 3447 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | maomao90 | 174 |
2 | awoo | 164 |
3 | adamant | 163 |
4 | TheScrasse | 159 |
5 | nor | 157 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 152 |
8 | Petr | 146 |
8 | orz | 146 |
10 | BledDest | 145 |
Название |
---|
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].