Pick The Optimization
dp[i][j]=mink<j{dp[i−1][k]+(pre[j]−pre[k])2}, with i up to 200, j up to 105, and all array values non-negative. Cheapest correct route?
The Aliens trick on i, one binary search per penalty. Expanding the square makes each k a line in pre[j] with slope −2pre[k] and intercept dp[i−1][k]+pre[k]2. A penalty per layer drops the i dimension entirely, so one hull is O(n) per probe and O(nlogV)≈6⋅106 overall.
Convex hull trick, one hull per layer. Expanding the square makes each k a line in pre[j] with slope −2pre[k] and intercept dp[i−1][k]+pre[k]2. Non-negative values make both the slopes and the query points monotone, so a deque hull is O(n) per layer and O(mn)≈2⋅107 overall.
Divide and conquer DP, one recursion per layer. Expanding the square makes each k a line in pre[j] with slope −2pre[k] and intercept dp[i−1][k]+pre[k]2. It is the only option here, since a squared cost is not Monge, so the recursion is O(mnlogn)≈3⋅108 overall.
Knuth optimization, one interval table per layer. Expanding the square makes each k a line in pre[j] with slope −2pre[k] and intercept dp[i−1][k]+pre[k]2. Quadrangle monotonicity collapses the split range, so the whole table is O(n2)≈1010 overall and fits the limit.
Sign in to answer questions and track your progress
Sign In