Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 321E Ciel and Gondola - The DP

State and transition

dp[g][i]dp[g][i] = minimum cost to assign first ii people to first gg gondolas. Transition: dp[g][i]=minj<i(dp[g1][j]+cost(j+1,i))dp[g][i] = \min_{j < i}(dp[g-1][j] + cost(j+1, i)) Here cost(l,r)cost(l, r) = number of knowing pairs in [l,r][l, r]. Precompute with 22D (two-dimensional) prefix sums. Naive: O(kn2)O(k \cdot n^2).

With D&C: O(knlogn)O(k \cdot n \log n). Work through examples by hand before coding. Understanding the pattern makes implementation simple. Practice with similar problems to reinforce your understanding.