[ad_1]
Dynamic programming (DP) is arguably crucial device in a aggressive programmer’s repertoire. There are a number of optimizations in DP that scale back the time complexity of ordinary DP procedures by a linear issue or extra, equivalent to Knuth’s optimization, Divide and Conquer optimization, the Convex Hull Trick, and so on. They’re, of paramount significance for superior aggressive programming, equivalent to on the degree of olympiads. On this article, we’ll uncover the divide and conquer optimization, NOT to be confused with the divide and conquer algorithm to resolve issues.
Divide and Conquer Optimization Standards:
The divide and conquer optimization can be utilized for issues with a dp transition of the next kind –
dp[i][j] = min_{1≤ok<j }(dp[i1][k1] + price[k][j])
Additional, the associated fee operate should fulfill the quadrangle inequality (QI), i.e.,
price(a, c) + price(b, d) ≤ price(a, d) + price(b, c) for all a ≤ b ≤ c ≤ d.
Divide and Conquer Optimization Approach:
The suboptimal strategy to resolve any downside with a dynamic programming transition of the shape given above would iterate by means of all potential values of ok < j for every transition. Then, if the issue constraints give 1 ≤ i ≤ m and 1 ≤ j ≤ n, the algorithm will take O(mn^{2}) time.
The important thing to the optimization is the next:
 Like in Knuth’s optimization, outline the operate choose(i, j), the minimal (or most, doesn’t matter) worth of ok for which dp[i][j] takes its minimal worth. Then, we’ve got the next relation:
choose[i][j] ≤ choose[i][j+1], the place
choose[i][j] = argmin_{ok<j}(dp[i1][k] + price[k][j])
Now, suppose we compute choose[i][j] for some i and j. Then, we additionally know that choose[i][p] ≤ choose[i][j] for all p < j. The suboptimal answer would contain looping for every j, by means of all potential values of ok for any fastened i. The optimization itself is as follows:
 Loop by means of the values of i, and first compute dp[i][j] and choose[i][j] for j = n/2, for the present i. That is potential as on the time of processing, we all know all of the values within the dp desk for dp[i1][k] for all ok ≤ n, because of the construction of the loop.
 Now, calculate dp[i][n/4] and dp[i][3n/4], realizing that choose[i][n/4] ≤ choose[i][n/2] and choose[i][n/2] ≤ choose[i][3n/4].
 We recursively name this clear up operate, maintaining observe of the decrease and higher bounds for choose[i][j] for some i and the present j. As an example, when calculating dp[i][5n/8], we all know that choose[i][n/2] ≤ choose[i][5n/8] ≤ choose[i][3n/4].
The algorithm is quicker by a linear issue as we don’t should loop by means of all values of ok, and a logarithmic issue is added because of the recursive nature of this algorithm. The time complexity is thus O(m * n * (log n)).
The generic code for this strategy is given beneath It makes use of a recursive strategy, which is the best to implement given the construction of the answer.
C++

Proof of Correctness of Divide and Conquer Optimization:
To show the correctness of this algorithm, we solely have to show the inequality –
choose[i][j] ≤ choose[i][j+1]
Comply with the beneath part for proof of correctness:
Assumptions: If price(i, j) satisfies the Quadrangle Inequality, then dp[i][j] additionally satisfies the inequality.
Now, contemplate the next setup –
 Now we have some indices 1 ≤ p ≤ q ≤ j and a separate fastened i.
 Let dp_{ok}[i][j] = price[k][i] + dp[k1][j1].
If we are able to present that –
dp_{p}[i][j] ≥ dp_{q}[i][j] ⇒ dp_{p}[i][j+1] ≥ dp_{q}[i][j+1]
then setting q = choose[i][j], it will likely be clear that choose[i][j+1] shall be at the least as a lot as choose[i][j], because of the implication of the above inequality for all indices p lower than choose[i][j]. It will show that choose[i][j1] ≤ choose[i][j].
Prrof:
From the Quadrangle inequality on the dp array we get –
price(p, j) + price(q, j+1) ≤ price(p, j+1) + price(q, j)
⇒ (dp[i1, p] + price(p, j)) + (dp[i1, q] + price(q, j+1)) ≤ (dp[i1, p] + price(p, j+1)) + (dp[j1, q] + price(q, j))
⇒ dp_{p}[i][j] + dp_{q}[i][j+1] ≤ dp_{p}[i][j+1] + dp_{q}[i][j]
⇒ dp_{p}[i][j] – dp_{q}[i][j] ≤ dp_{p}[i][j+1] – dp_{q}[i][j+1]dp_{p}[i][j] ≥ dp_{q}[i][j]
⇒ 0 ≤ dp_{p}[i][j] – dp_{q}[i][j] ≤ dp_{p}[i][j+1] – dp_{q}[i][j+1]
⇒ dp_{p}[i][j+1] ≥ dp_{q}[i][j+1]This completes the proof dp_{p}[i][j] ≥ dp_{q}[i][j] ⇒ dp_{p}[i][j+1] ≥ dp_{q}[i][j+1]
Examples to Present Working of Divide and Conquer Optimization:
Given an array arr[] of N components, the duty is to divide it into Ok subarrays, such that the sum of the squares of the subarray sums is minimized.
Examples:
Enter: arr []= {1, 3, 2, 6, 7, 4}, Ok = 3.
Output: 193
Rationalization: The optimum division into subarrays is [1, 3, 2], [6] and [7, 4],
Giving a complete sum of (1 + 3 + 2)^{2} + (6)^{2} + (7 + 4)^{2 }= 193.
That is the minimal potential sum for this explicit array and Ok.Enter: arr[] = {1, 4, 2, 3}, Ok = 2
Output: 50
Rationalization: Divide it into subarrays {1, 4} and {2, 3}.
The sum is (1+4)^{2} + (2 + 3)^{2} = 5^{2} + 5^{2} = 50.
That is the minimal potential sum.
Suboptimal answer: The issue will be solved primarily based on the next concept:
 If first j1 components are divided into i1 teams then the minimal price of dividing first j components into i teams is identical because the minimal worth amongst all potential mixture of dividing first k1 (i ≤ ok ≤ j) components into i1 teams and the price of the ith group shaped by taking components from ok^{th} to j^{th} indices.
 Let dp[i][j] be the minimal sum obtainable by dividing the primary j components into i subarrays.
So the dptransition shall be –dp[i][j] = min_{i≤ok≤j }(dp[i1][k1] + price[k][i])
the place price[k][i] denotes the sq. of the sum of all components within the subarray arr[k, k+1 . . . i]
Comply with the steps talked about beneath for fixing the issue:
 The associated fee operate will be calculated in fixed time by preprocessing utilizing a prefix sum array:
 Calculate prefix sum (say saved in pref[] array).
 So price(i, j) will be calculated as (pref[j] – pref[i1]).
 Traverse from i = 1 to M:
 Traverse from j = i to N:
 Traverse utilizing ok and kind the dp[][] desk utilizing the above dp remark.
 The worth at dp[M1][N1] is the required reply.
Beneath is the implementation of the above strategy.
C++

Time Complexity: O(M * N^{2})
Auxiliary Area: O(M * N)
Optimum Resolution (Utilizing Divide and Conquer Optimization):
This downside follows the quadrangle We are able to, nevertheless, discover that the associated fee operate satisfies the quadrangle inequality
price(a, c) + price(b, d) ≤ price(a, d) + price(b, c).
The next is the proof:
Let sum(p, q) denote the sum of values in vary [p, q] (subarray of arr[[]), and let x = sum(b, c), y = sum(a, c) − sum(b, c), and z = sum(b, d) − sum(b, c).
Utilizing this notation, the quadrangle inequality turns into
(x + y)^{2} + (x + z)^{2} ≤ (x + y + z)^{2} + x^{2},
which is equal to 0 ≤ 2yz.Since y and z are nonnegative values, this completes the proof. We are able to thus use the divide and conquer optimization.
 There’s yet one more layer of optimization within the house complexity that we are able to do. To calculate the dp[][] states for a sure worth of j, we solely want the values of the dp state for j1.
 Thus, sustaining 2 arrays of size N and swapping them after the dp[][] array has been crammed for the present worth of j removes an element of Ok from the house complexity.
Observe: This optimization can be utilized for all implementations of the divide and conquer DP speedup.
Comply with the steps talked about beneath to implement the thought:
 The associated fee operate will be calculated utilizing prefix sum as within the earlier strategy.
 Now for every fastened worth of i (variety of subarrays during which the array is split):
 Traverse the entire array to seek out the minimal potential sum for i divisions.
 The worth saved in dp[M%2][N1] is the required reply.
Beneath is the implementation of the above strategy.
C++

Time Complexity: O(M * N * logN)
Auxiliary Area: O(N)
[ad_2]