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 is a cost function and you can compute it in time. Furthermore, for .
The straightforward implementation gives a runtime of if and . Divide & Conquer DP allows this to be optimized to .
For each , let be the value of 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 . First, compute . Then compute using the fact that it is less than or equal to . Similarly, we can compute and recursively split the ranges in half, keeping track on the lower and upper bounds.
Since each possible value of appears times, this gives a final runtime of .
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 denote the location of the last door if we place doors optimally among the first rooms. The idea is that . Assume this was not true for the sake of contradiction. Then , so we also have . But then we could have used the best possible setup for in the setup as well, since all open doors are among the first rooms anyways.
Since the monotonicity condition is held, we can now perform Divide & Conquer DP. Fix the value of and compute . Then compute it for the left and right halves of the array.
Implementation
Time Complexity: , since we need to check 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 . 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!