# Divide & Conquer - DP

Authors: Andi Qu, Benjamin Qi

Using Divide & Conquer as a DP Optimization.

### Prerequisites

## Overview

Resources | ||||
---|---|---|---|---|

cp-algo | ||||

Jeffrey Xiao | ||||

GCP |

Consider a dynamic programming problem with the following formula

where $C(i,j)$ is a cost function and you can compute it in $O(1)$ time. Furthermore, $dp(i,j) =0$ for $j<0$.

The straightforward implementation gives a runtime of $O(MN^2)$ if $0\leq i < M$ and $0\leq j < N$. Divide & Conquer DP allows this to be optimized to $O(M N \log N)$.

For each $i,j$, let $\text{opt}(i,j)$ be the value of $k$ that minimizes the right hand side of the equation.
Divide & Conquer DP **only applies if**

Often, proving this with the given cost function is challenging, but if the cost function satisfies the quadrangle inequality, the condition holds.

We can then apply the idea behind Divide & Conquer. Fix a given $i$. First, compute $\text{opt}(i,n/2)$. Then compute $\text{opt}(i, n/4)$ using the fact that it is less than or equal to $\text{opt}(i, n/2)$. Similarly, we can compute $\text{opt}(i, 3n/4)$ and recursively split the ranges in half, keeping track on the lower and upper bounds.

Since each possible value of $\text{opt}(i, j)$ appears $O(\log n)$ times, this gives a final runtime of $O(mn \log n)$.

## Example - Circular Barn

Focus Problem – try your best to solve this problem before continuing!

View Internal SolutionYou should already be familiar with the CHT solution.

### Explanation

We iterate through the possibilities of the location of the first door. For each of the first doors, we can now view the barn linearly. All further calculations will be done assuming the barn is a linear sequence of doors starting at the first opened door.

Let $dp(i,k)$ denote the location of the last door if we place $k$ doors optimally among the first $i$ rooms. The idea is that $dp(i,k) \leq dp(i+1, k)$. Assume this was not true for the sake of contradiction. Then $dp(i,k) > dp(i+1,k)$, so we also have $dp(i+1,k) \leq i$. But then we could have used the best possible setup for $(i+1,k)$ in the $(i,k)$ setup as well, since all open doors are among the first $i$ rooms anyways.

Since the monotonicity condition is held, we can now perform Divide & Conquer DP. Fix the value of $k$ and compute $dp(n/2, k)$. Then compute it for the left and right halves of the array.

### Implementation

**Time Complexity:** $\mathcal{O}(n^2 k \log n)$, since we need to check $n$ rooms for the optimal first barn position.

C++

#include <bits/stdc++.h>using namespace std;#define ll long longconst int MAX_N = 1000;const int MAX_K = 7;// calc[i][j] stores the # of steps to get all cows distance j away to door i// to make implementing a cyclic array easier, we double the sizevector<vector<ll>> calc(2 * MAX_N, vector<ll>(MAX_N + 1, 0));

## Problems

Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|

CEOI | Normal | ## Show TagsD&C, DP | |||

COI | Normal | ## Show TagsD&C, DP | |||

CF | Normal | ## Show TagsD&C, DP | |||

AC | Normal | ## Show TagsD&C, DP | |||

CF | Hard | ## Show TagsD&C, DP | |||

CF | Hard | ## Show TagsD&C, DP | |||

POI | Very Hard | ## Show TagsD&C, DP | |||

IOI | Very Hard | ## Show TagsD&C, DP | |||

Platinum | Very Hard | ## Show TagsD&C, DP | |||

JOI | Very Hard | ## Show TagsD&C, DP |

JOI Bubblesort English Statement: You are given an array of length $N$ $(1 \le N \le 100,000)$. You must choose two numbers in this array and swap them. After swapping these two numbers, you sort the array using a bubble sort algorithm. What is the minimum number of bubble sort swaps necessary, assuming you choose the two initial numbers to swap optimally? The two initial numbers that you swap do not count towards the minimum number of bubble sort swaps.

### Module Progress:

### Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!