Divide and Conquer DP¶
Divide and Conquer is a dynamic programming optimization.
Preconditions¶
Some dynamic programming problems have a recurrence of this form:
where
Say
Let
This lets us solve for all states more efficiently. Say we compute
To minimize the runtime, we apply the idea behind divide and conquer. First,
compute
Note that it doesn't matter how "balanced"
Generic implementation¶
Even though implementation varies based on problem, here's a fairly generic
template.
The function compute
computes one row dp_cur
, given the previous row dp_before
.
It has to be called with compute(0, n-1, 0, n-1)
. The function solve
computes m
rows and returns the result.
int m, n;
vector<long long> dp_before, dp_cur;
long long C(int i, int j);
// compute dp_cur[l], ... dp_cur[r] (inclusive)
void compute(int l, int r, int optl, int optr) {
if (l > r)
return;
int mid = (l + r) >> 1;
pair<long long, int> best = {LLONG_MAX, -1};
for (int k = optl; k <= min(mid, optr); k++) {
best = min(best, {(k ? dp_before[k - 1] : 0) + C(k, mid), k});
}
dp_cur[mid] = best.first;
int opt = best.second;
compute(l, mid - 1, optl, opt);
compute(mid + 1, r, opt, optr);
}
long long solve() {
dp_before.assign(n,0);
dp_cur.assign(n,0);
for (int i = 0; i < n; i++)
dp_before[i] = C(0, i);
for (int i = 1; i < m; i++) {
compute(0, n - 1, 0, n - 1);
dp_before = dp_cur;
}
return dp_before[n - 1];
}
Things to look out for¶
The greatest difficulty with Divide and Conquer DP problems is proving the
monotonicity of
Practice Problems¶
- AtCoder - Yakiniku Restaurants
- CodeForces - Ciel and Gondolas (Be careful with I/O!)
- CodeForces - Levels And Regions
- CodeForces - Partition Game
- CodeForces - The Bakery
- CodeForces - Yet Another Minimization Problem
- Codechef - CHEFAOR
- CodeForces - GUARDS (This is the exact problem in this article.)
- Hackerrank - Guardians of the Lunatics
- Hackerrank - Mining
- Kattis - Money (ACM ICPC World Finals 2017)
- SPOJ - ADAMOLD
- SPOJ - LARMY
- SPOJ - NKLEAVES
- Timus - Bicolored Horses
- USACO - Circular Barn
- UVA - Arranging Heaps
- UVA - Naming Babies