NeFut Logo NeFut
Admin Login

Dynamic Programming: In-Depth Analysis and Practical Applications

Published at: 2026-05-27 09:17 Last updated: 2026-06-06 13:04
#algorithm #Dynamic Programming #Math

Core Logic and Mathematical Principles

Dynamic Programming (DP) is not a specific code template, but a mathematical concept that involves topological sorting and pruning of state spaces. Its essence lies in decomposing a complex problem into several overlapping subproblems, saving the solutions of subproblems to avoid redundant calculations, ultimately leading to a global optimal solution.

The feasibility of DP is determined by three rigid mathematical criteria:

  1. Optimal Substructure: The optimal solution of the global problem contains the optimal solutions of the subproblems. If we define a state $S$, its optimal value $V(S)$ must be formed by combining the optimal values $V(S')$ of its subset $\{S'\}$ through some monotonic operator (such as $\min, \max, +$).
  2. No-aftereffect: This is the key to the validity of DP. Once the current state is determined, the evolution of all subsequent states depends only on the current state value, regardless of the path taken to reach that state (i.e., historical decisions). In graph theory terms, the state space must be mappable to a Directed Acyclic Graph (DAG), where states are vertices and decisions are directed edges.
  3. Overlapping Subproblems: Different decision paths may reach the same substate. If the subproblem space is exponential, but the deduplicated actual state space is polynomial, then caching subproblem solutions (deduplication of solution space) can force the time complexity down from $O(2^N)$ or $O(N!)$ to $O(|V| + |E|)$.

State Design and Algorithm Derivation

Taking the classic linear recurrence problem as an example. Designing DP requires completing three hardcore derivations:

1. State Space Definition

Let $f[i]$ represent the local optimal solution after eliminating the first $i$ decision dimensions. Each dimension of the state must precisely correspond to boundary constraints and must completely obscure the historical path leading to that state.

2. State Transition Equation (Mathematical Mapping)

The state transition is essentially an edge weight relaxation operation on the DAG. For the target state $i$, its predecessor state set is $prev(i)$, and the transition cost is $w(j, i)$, thus the state transition equation is:

$$f[i] = \max_{j \in prev(i)} \{ f[j] + w(j, i) \}$$

3. Boundary Conditions and Topological Order

It is essential to clearly define the benchmark state (Base Case) without any predecessors, typically the initial assignments of $f[0]$ or $f[1]$. The computation of the entire state space must strictly follow the topological order of the DAG to ensure that when computing $f[i]$, all $j \in prev(i)$ have their $f[j]$ already solidified.


C++ Standard Source Code (NOIP Style)

The following is the core implementation of the Longest Increasing Subsequence (LIS) with a time complexity of $O(N^2)$, strictly adhering to the NOIP examination Linux environment compilation standards.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 5005;
int a[MAXN];
int f[MAXN];

int main() {
    // Optimize standard input/output streams, sever synchronization with stdin/stdout to speed up I/O
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    if (!(cin >> n)) return 0;

    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }

    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        f[i] = 1; // Boundary condition: itself forms an independent team, length at least 1
        for (int j = 1; j < i; ++j) {
            if (a[j] < a[i]) {
                f[i] = max(f[i], f[j] + 1); // State transition: transferred from predecessor states satisfying no-aftereffect conditions
            }
        }
        ans = max(ans, f[i]); // Maintain global optimal solution
    }

    cout << ans << "\n";

    return 0;
}

NOIP Practical Pitfall Guide

1. Uninitialized State Array or Boundary Overflow

2. Aftereffect Pollution


Classic NOIP/Luogu Problems

1. Luogu P1048 [NOIP2005 Popular Group] Herb Picking

2. Luogu P1091 [NOIP2004 Advanced Group] Choir Formation

Original Source: local://13.1

[h] Back to Home