Блог пользователя icode_247

Автор icode_247, история, 6 лет назад, По-английски

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.

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

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