Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 674C Levels and Regions - Standard DP

Before optimization

Standard DP: dp[i][j]dp[i][j] = minimum expected time to complete first ii levels using exactly jj regions. Transition (the formula to compute each state): dp[i][j]=minp<i(dp[p][j1]+cost(p+1,i))dp[i][j] = \min_{p < i}(dp[p][j-1] + cost(p+1, i)) where cost(l,r)cost(l, r) is the expected time to complete levels ll through rr as a single region.

Time complexity: O(n2k)O(n^2 k).

Space complexity: O(n)O(n) for the dp array. With n=200,000n = 200{,}000 and k=50k = 50, that's 2×10122 \times 10^{12} operations. Way too slow. Even O(n2)O(n^2) transitions are borderline at 4×10104 \times 10^{10} operations.