Dynamic Programming21 sections · 915 units
Open in Course

Codeforces 674C Levels and Regions - CHT Combination

Going even faster

The transition dpλ[i]=min⁡p(dpλ[p]+cost(p+1,i)+λ)dp_{\lambda}[i] = \min_{p}(dp_{\lambda}[p] + cost(p+1, i) + \lambda) often has structure that allows further improvement with Convex Hull Trick (covered in Section 1616).

If cost(p+1,i)cost(p+1, i) can be written as A[p]⋅B[i]+C[p]+D[i]A[p] \cdot B[i] + C[p] + D[i] (separable into terms depending only on pp and terms depending only on ii), CHT applies. Each transition becomes a line query.

This problem achieves O(nlog⁡nlog⁡C)O(n \log n \log C) by combining Aliens Trick with CHT. First Aliens removes the kk dimension, then CHT speeds up the O(n2)O(n^2) transitions to O(nlog⁡n)O(n \log n). The techniques compose beautifully.