what do dp[i] represents?

Revision en1, by melikeairplanes, 2025-06-28 17:35:16

I learnt dynamic programming from here striver and this guy solves all problems using a same pattern, where dp represents the final answer of the sub-problem(ex — in fibonacci, dp[i] represents fibo number at index i), I did around 50 questions(easy, med and hard) from the same source. Now, when I started dp questions on codeforces, I have seen something new, were dp do not represent the answer of subproblem, rather than there is a way of finding answer from dp, like this 2061C - Кевин и головоломка, int this, my approach was to define dp[i][lr] = number of possible ways till index i (**without** any condition on if the person in index i is liar or honest) if there are lr number of liars, which corresponds to what I learnt from youtube, but the solution says dp[i] = nunmber of possible ways considering ith person is honest and at last add dp[n]+dp[n-1]. Another problem like this is 455A - Скука, in which the dp[i] don't represents answer till index i(my first approach), rather dp[i] represents the longest length if the max element is i, and many other question Shayan's Topic streams. He is also doing the same, many times, dp do not directly represents the answer, but answer can be calculated via dp. How and where to learn this? How to decide the states? I able to questions almost every time in contests where dp represents the direct answer of the subproblem, but whenever there is a problem where dp do not represent the answer, my brain stops, and when i see the editorial, I am always disappointed, is there some trick or else i don't know or else. How can i learn this? Help me :) please.

Tags dp, question

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English melikeairplanes 2025-06-28 17:35:16 1714 Initial revision (published)