Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 977F Consecutive Subsequence - State Definition - 2

Extra requirements for state definition.

As I mentioned earlier, you need something else as well to reconstruct the subsequence:

lastpos[v]lastpos[v] = The index ii of the array where dp[v]dp[v] is achieved.

prev[i]prev[i] = For index ii, prev[i]prev[i] is the previous index in the subsequence ending at ii. If the subsequence starts at index i, then prev[i] = -1.

Choosing the right state (what each dp value represents) is often the hardest part. The state must capture everything needed to solve the subproblem independently.

Before moving to the next unit, try to think: if dp[x]dp[x] is "best subsequence ending at value xx", how should you update it when you see a new element x=aix = a_i in the array?