icode_247's blog

By icode_247, history, 6 years ago, In English

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.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it +8 Vote: I do not like it

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].