Dynamic Programming21 sections · 916 units
Open in Course

Sonya - Walkthrough

Tracing the heap operations

Make array non-decreasing with minimum cost (sum of changes). Transform: bi=aiib_i = a_i - i. Now make bb non-decreasing but with the same values allowed. dp[i][x]dp[i][x] = min cost for first ii elements, last value is xx.

Transition: dp[i][x]=minyx(dp[i1][y])+xbidp[i][x] = \min_{y \leq x}(dp[i-1][y]) + |x - b_i|. The minyx\min_{y \leq x} is a prefix min. The xbi|x - b_i| adds a V-shape centered at bib_i. Heap operations: prefix min=removemin = remove all right breakpoints beyond the min point. Add V=addV = add bib_i to both heaps, balance.