Dynamic Programming21 sections · 916 units
Open in Course

Sonya - Transform Trick

Making it non-decreasing

Strictly increasing means a1<a2<<ana_1 < a_2 < \ldots < a_n. This is hard to work with. Transform it: let bi=aiib_i = a_i - i. Now you want bb to be non-decreasing instead.

Why? If b1b2bnb_1 \leq b_2 \leq \ldots \leq b_n, then a1<a2<<ana_1 < a_2 < \ldots < a_n automatically. The cost stays the same: changing aia_i to xx is the same as changing bib_i to xix - i. This transform converts strict inequality to non-strict. Now you can use the classic non-decreasing array technique with heaps.