Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 674C Levels and Regions - Aliens Trick

Removing the k dimension

Add penalty λ\lambda per region. Penalized DP: dpλ[i]dp_{\lambda}[i] = min cost to partition first ii levels into any number of regions, with penalty. Transition: dpλ[i]=minp<i(dpλ[p]+cost(p+1,i)+λ)dp_{\lambda}[i] = \min_{p < i}(dp_{\lambda}[p] + cost(p+1, i) + \lambda). The +λ+\lambda is the per-region penalty.

Now you have O(n)O(n) states (one per position). If cost(l,r)cost(l, r) can be computed in O(1)O(1) with preprocessing, total time is O(n2)O(n^2) per binary search iteration, or O(n2logC)O(n^2 \log C) overall.