Knuth's Optimization
Introduction
| Resources | |||||
|---|---|---|---|---|---|
| Jeffrey Xiao | |||||
| GCP | |||||
| GFG | Good explanation + Proof of correctness | ||||
Knuth's optimization is a special case of Range DP. In general, it is used to solve DP problems with transitions of the form
Further, the cost function must satisfy the following conditions for all :
- (the quadrangle inequality)
Define be the index for which takes on its minimum value, or equivalently,
If more than one such index exists, then take the minimum (or the maximum, doesn't matter). Then, assuming conditions on the dp transition and the cost function to be satisfied, we can show that satisfies
The proof of correctness for this claim is in the resources above.
The structure of the code is identical to that of Range DP, with the exception of maintaining the array. To calculate and , it is only necessary to check values of between and . Because of the monotonicity of , the time complexity of the final algorithm can be shown to be .
Problems
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| CF | Easy | Show TagsDP | ||||
| SPOJ | Medium | Show TagsDP | ||||
| onlinejudge.org | Medium | Show TagsDP | ||||
| onlinejudge.org | Hard | Show TagsDP | ||||
Connected-Component DP
Focus Problem – try your best to solve this problem before continuing!
View Internal SolutionIntroduction
| Resources | |||||
|---|---|---|---|---|---|
| CF | Miscellaneous techniques | ||||
| CF | |||||
Let's define a "connected component" as an array of elements with a specific order. We will surround these with brackets. For example, and are connected components.
Let's define a "collection" of connected components as several non-adjacent connected components without any order. We will surround these with braces. For example, is a collection of 4 connected components. is another representation of the same collection.
In connected component DP, we can use these components to create a structure given some requirements. This often appears with permutation-related problems.
Connected component DP involves the state as some quantity with the first elements arranged in connected components. There are three main transitions when adding a new element to the collection.
- Add it as a new component. Number of components increases.
- Add it onto the end of an existing component. Number of components remains same.
- Add it in between two components to merge them. Number of components decreases.
The quantity for a complete array is , or the quantity after arranging of all elements into component.
Solution - Counting Permutations II
Time Complexity:
This problem requires us to store additional information. Define as the number of valid configurations of the numbers from to into connected components, where value has borders where a border is a side lacking a neighbor. For instance, the collection would be counted in the state .
- If has no borders, then must point two existing connected components.
- If has one border, then must be appended on to an existing connected component.
- If has two borders, it must be inserted as its own connected component.
This corresponds to the following transitions:
The base case is , and the final answer is .
#include <bits/stdc++.h>const int MOD = 1000000007;int main() {int N;std::cin >> N;std::vector dp(N + 1, std::vector<std::array<long long, 3>>(N + 2));for (auto &a : dp)for (auto &b : a) b.fill(0);
Problems
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| CEOI | Medium | Show TagsDP | ||||
| CF | Medium | Show TagsDP | ||||
| JOI | Medium | Show TagsDP | ||||
| CF | Medium | Show TagsDP | ||||
| CF | Medium | Show TagsDP | ||||
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!