Let's define:

  • g(l,r)g(l, r) as the said cost to reduce A[lr]A[l \dots r] sub-array to a single element. g(1,n)g(1, n) will be our result for the whole array.

  • f(l,r,z)f(l, r, z) as the cost to shrink A[lr]A[l \dots r] sub-array to a zz-sized sub-array, where zrl+1z \le r-l+1.

By definition, f(l,r,1)=g(l,r)f(l, r, 1) = g(l, r).

For a segment [l,r][l, r] and lp<rl \le p < r, we can pretend that [l,p][l, p] will reduce to a single element and [p+1,r][p+1, r] can shrink down to some zz elements where 1z<k1 \le z < k.

Ultimately we can merge this z+1z + 1 new elements into one and get the cost for [l,r][l, r], which is g(l,r)g(l, r). Formally, we can write:

g(l,r)=i=lrAi+minp{ g(l,p)+minz{ f(p+1,r,z) } }g(l, r) = \sum\limits_{i=l}^{r} A_i + \min\limits_p \{~ g(l, p) + \min\limits_z \{~ f(p+1, r, z) ~\} ~\}

Similary, we can formally define f(l,r,z)f(l, r, z) as:

f(l,r,z)=minp{g(l,p)+f(p+1,r,z1)}f(l, r, z) = \min\limits_p \{ g(l, p) + f(p + 1, r, z - 1) \}

We can use prefix-minimum array to store minz{ f(l,r,z) }\min\limits_z \{~ f(l, r, z) ~\} as we calculate f(l,r,z)f(l, r, z).

Thus our overall complexity is O(n3k)O(n^3k) per test case.

Statistics

26% Solution Ratio
MrBrionixEarliest, Jul '22
MrBrionixFastest, 0.3s
MrBrionixLightest, 1.6 MB
MrBrionixShortest, 1025B
Toph uses cookies. By continuing you agree to our Cookie Policy.