This blog is not exactly a tutorial, it is more of a review of the techniques that can be used solve some DP problems involving the quadrangle inequality, and some problems related to DNC DP.
DNC DP
DNC DP is a well-known optimisation that can solve some DP problems from $$$O(N^2K)$$$ time to $$$O(NKlogN)$$$ time. The key idea uses the monotonic nature of the splitting points to compute each layer in $$$O(NlogN)$$$ time. In other words, it uses the fact that $$$opt(N,K)<=opt(N+1,K)$$$, where $$$opt(N,K)$$$ is defined as the left splitting point for the $$$K^{th}$$$ segment for the first $$$N$$$ elements, for all positive integers $$$N$$$ and $$$K$$$. Most often, the monotonic nature is satisfied when the cost function satisfies the quadrangle inequality (a.k.a. wider is worse), which is for all indices $$$A<=B<=C<=D$$$, $$$cost(A,C)+cost(B,D)>=cost(A,D)+cost(B,C)$$$ (it can also work the other way).
Guards
Given an array of length $$$N$$$, split it into $$$K$$$ segments such that $$$\sum_{i=1}^K$$$ (length $$$\cdot$$$ sum) is minimised. This problem can be considered classic, and has been explored by other guides talking about DNC DP. To solve, just notice that the cost function satisfies the quadrangle inequality. Convince yourself that it is true. Hence, we can use DNC DP on this problem.
The code is actually pretty simple, and can be found here.
Monge + 1D-1D DP
Two other implications of quadrangle inequality are:
- Monge can be done
- 1D-1D DP Optimisation can be done
By combining these two techniques, we can solve some DNC DP problems in $$$O(Nlog^2N)$$$ time, cutting out the $$$K$$$ factor. Let's now explain how this works.
First, we apply Monge, and transform the problem into:
Given an array of length $$$N$$$ and a cost $$$K$$$ for each segment, split it into as many segments as you want, such that $$$\sum_{i=1}^X$$$ (length $$$\cdot$$$ sum) is minimised, where $$$X$$$ is the number of segments in the end.
Our DP function becomes $$$dp[i] = \min_{1 \leq j \leq i} \{ dp(j-1) + C(j, i) + K \}$$$ which is then a function that can be solved in $$$O(NlogN)$$$ time using 1D-1D DP optimisation. Read the dedicated blog for more details. Since we solve this problem at most $$$O(logN)$$$ times due to our use of Monge, hence we achieve an $$$O(Nlog^2N)$$$ time complexity, beating DNC DP.
A sample implementation of this for Guards is provided here.
This technique is known, I first learnt about it when browsing the fastest submission on 321E - Ciel and Gondolas. However, a major caveat of this method is that the cost function must be able to be computed in $$$O(1)$$$/very quickly, or else there will be issues, seen in the next section.
When Monge Fails
Consider 868F - Yet Another Minimization Problem. We can use DNC DP to solve this, but we need to slightly modify it. Because computing the cost function is no longer $$$O(1)$$$, we need to use Mo-like two pointer method to move them to a desired range to compute our costs. In the end, the total number of moves we need to make is bounded by $$$O(NKlogN)$$$, due to how the structure of DNC DP works. However, if we compare this to 1D-1D DP, the structure with which we compute the answers do not help. The best we can do is $$$O(Nlog^2N\sqrt{N})$$$. Combined with the fact that this has bad constant time, we end up with a more complex and also more expensive solution than DNC DP. This sort of problem still gives DNC DP its use.
Although not common, there is one example of a problem that does not satisfy quadrangle inequality but works with DNC DP (although not optimal). problem
Given the numbers $$$1$$$ to $$$N$$$, you can guess a number $$$X$$$. This guess costs $$$X$$$ and you will be told if your guess is higher than, lower than or equal to the correct answer. The final cost is the sum of costs before you get the equal answer. Find the minimal final cost of the worst case in a series of guesses.
There exists a simpler $$$O(N^2)$$$ solution, but we shall explore the DNC DP solution.
Define $$$dp(N,K)$$$ as the minimal cost of the segment $$$[1+K,2+K,3+K,...N+K]$$$. Then, we get $$$dp(N,K) = \min_{1 \leq i \leq N} \{ dp(i-1,K) + dp(N-i,K+i) + i + K \}$$$.
If we set $$$opt(N,K)$$$ as the optimal choice of $$$i$$$ for $$$dp(N,K)$$$, then we have that $$$opt(N,K)>=opt(N,K+1)$$$. In other words, I claim that the optimal choice of a splitting point starts from the right and shift towards the centre monotonically. This can be proven rigorously, but I am lazy. Intuitively, the idea is that as $$$K$$$ increases, the more that $$$N$$$ doesn't matter, since the elements become more and more "close". Hence, the optimal strategy tends towards a binary search. When $$$K$$$ is low compared to $$$N$$$, the elements have a much larger difference in magnitude, and it is preferred to start more towards the right. Because of this property, we can apply DNC DP to solve this problem.
For this problem, it is not clear how Monge + 1D-1D DP may be used, even though DNC DP is usable. Do you know of any other problems that are like this?
Conclusion
Although Monge + 1D-1D DP is more powerful than DNC DP when cost function can be computed in $$$O(1)$$$ time, DNC DP is not totally useless. Hopefully, we will see more interesting problems on DNC DP in the future, which have more steps before applying the classic algorithm.
D&C, not DNC...
to me, both forms are fine, it is just personal preference that i use DNC over D&C
Like this comment if you are not racist.
DaC gang here
/&XP gang here
I just wanna ask if these techniques may appear in Div2 E or lower problems. I've solved a few D&C DP, CHT problems and I'm pretty sure I can solve most problems requiring D&C DP or CHT at its simplest form. So right now, should I learn these tricks (Alien Trick, Monge, 1D-1D) or should I focus more on practicing problems at or slightly above my skill level (~1900-2100)? My aim right now is to just consistently solving D and sometimes E Div2. Thanks in advance!
Most recent DNC DP problem I have seen on CF is 1715E - Long Way Home, which is >1 year ago. These techniques will appear easiest at D2E, even then it's uncommon, so it would be better use of time to practice things that are slightly above your skill.
Just to add on, these DNC and monge techniques aren't actually unique to DP problems in general. Though they definitely appear a lot more in DP than in non-DP problems
DNC example:
https://oj.uz/problem/view/JOI19_cake3
For monge, there's supposedly this classic problem from https://robert1003.github.io/2020/02/26/dp-opt-wqs-binary-search.html#a-hrefhttpswwwlydsycomjudgeonlineproblemphpid2654bzoj2654---treea