Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 977F Consecutive Subsequence - Transition

Learn the transition formula: extend subsequences by linking consecutive values.

2. Transition

For each element a[i] = x:

\Rightarrow Compute the best subsequence that can end at x

  • leni=dp[x1]+1len_i = dp[x-1] + 1

\Rightarrow Set the predecessor

  • If a subsequence ended at x-1, prev[i] = last_pos[x - 1]
  • Otherwise, this starts a new subsequence: prev[i] = -1

\Rightarrow Update dp[xx] if you improved it

If this new subsequence is better:

  • dp[x] = len_i
  • last_pos[x] = i

\Rightarrow Update global best

If dp[x] is the largest seen:

  • best_len = dp[x]
  • best_end = i