Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 321E Ciel and Gondola - Walkthrough

Tracing the optimization

Partition nn people into kk groups to reduce total cost. Cost of group [l,r][l, r] depends on prefix sums. dp[j][i]dp[j][i] = min cost to partition first ii people into jj groups.

Transition: try all splits. QI holds for the cost function. Apply D&C: for layer jj, compute all dp[j][i]dp[j][i] in O(nlogn)O(n \log n) using the recursion. Total: O(knlogn)O(kn \log n) instead of O(kn2)O(kn^2). For k=100k = 100, n=105n = 10^5, this is the difference between TLE and AC.