---
id: 2701
lang: en
translation_of: index.md
source_sha256: 951047ae2a69b25af569b5e87a36966d7b4caa6746b970ba47313b803dfd523f
title: "NOI Review Topic - DP"
description: "NOI review notes on DP: slope optimization, decision monotonicity, and a few standard tricks for unrooted trees and sqrt divide and conquer."
tags: ["OI","DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "两维都不连续 rendered literally as “neither dimension of a point is continuous” (the author means neither coordinate is monotonic)."
---

## Slope Optimization

Slope optimization actually isn't very hard. You can think of it as taking some points in the plane as decision points; you now have a line with slope $k$, and you need to make it pass through a suitable point so that the line's intercept is maximized/minimized. Clearly this can be done by making these points form an upper/lower convex hull, then binary searching for the segment with the closest slope and transitioning from it.

<!--more-->

An example problem where neither dimension of a point is continuous: *[NOI2007] Currency Exchange*. It needs CDQ divide and conquer. Of course, you could also force it online so that only Splay works <del>(of course, you could also use binary grouping, take an extra $\Theta(\log)$, and <span class="slang" data-zh="艹过去" data-gloss="cào guòqù, “f*ck through”: 艹 stands in for the vulgar 操 (cào). Crudely forcing a solution through until it gets accepted.">f*ck your way through it</span>)</del>

```cpp
// P4027.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200;
const double eps = 1e-7;

int n, q[MAX_N], top;
double SA[MAX_N], SB[MAX_N], rate[MAX_N], f[MAX_N];

struct node
{
    double x, y;
    int id;
} nodes[MAX_N], tmp[MAX_N];

double slope(int i, int j)
{
    if (fabs(nodes[i].x - nodes[j].x) < eps)
        return 1e-15;
    return (nodes[i].y - nodes[j].y) / (nodes[i].x - nodes[j].x);
}

double calc(int i, int j)
{
    double VB = f[j] / (rate[j] * SA[j] + SB[j]);
    return SA[i] * VB * rate[j] + SB[i] * VB;
}

int binary_search(double k)
{
    int l = 1, r = top - 1, res = top;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        if (slope(q[mid], q[mid + 1]) <= k)
            res = mid, r = mid - 1;
        else
            l = mid + 1;
    }
    return q[res];
}

void solve(int l, int r)
{
    if (l == r)
    {
        f[l] = max(f[l], f[l - 1]), nodes[l].id = l;
        nodes[l].y = f[l] / (rate[l] * SA[l] + SB[l]);
        nodes[l].x = nodes[l].y * rate[l];
        return;
    }
    int mid = (l + r) >> 1;
    solve(l, mid), top = 0;
    for (int i = l; i <= mid; i++)
    {
        while (top > 1 && slope(q[top - 1], q[top]) <= slope(q[top - 1], i))
            top--;
        q[++top] = i;
    }
    for (int i = mid + 1; i <= r; i++)
        f[i] = max(f[i], calc(i, nodes[binary_search(-SA[i] / SB[i])].id));
    solve(mid + 1, r);
    int lptr = l, rptr = mid + 1, ptr = l;
    while (lptr <= mid && rptr <= r)
        if (nodes[lptr].x < nodes[rptr].x || (fabs(nodes[lptr].x - nodes[rptr].x) < 1e-9 && nodes[lptr].y < nodes[rptr].y))
            tmp[ptr] = nodes[lptr], lptr++, ptr++;
        else
            tmp[ptr] = nodes[rptr], rptr++, ptr++;
    while (lptr <= mid)
        tmp[ptr] = nodes[lptr], lptr++, ptr++;
    while (rptr <= r)
        tmp[ptr] = nodes[rptr], rptr++, ptr++;
    for (int i = l; i <= r; i++)
        nodes[i] = tmp[i];
}

int main()
{
    scanf("%d%lf", &n, &f[0]);
    for (int i = 1; i <= n; i++)
        scanf("%lf%lf%lf", &SA[i], &SB[i], &rate[i]), f[i] = f[0];
    solve(1, n), printf("%.10lf\n", f[n]);
    return 0;
}
```

## Decision Monotonicity

A very classic example problem: *LibreOJ #6039. [Yali Training Camp 2017 Day5] Jewelry*. It also contains a <span class="slang" data-zh="套路" data-gloss="tàolù, “routine, set pattern”. OI slang for a standard, well-known trick that works on a whole class of problems.">standard trick</span> for knapsack DP.

First, the proof of this is easy. For each fixed price $c$, the transition is as follows:

$$
dp[i] = \max_{j \bmod c = i \bmod c, j < i} dp[j] + pre[\frac{i - j}{c}]
$$

Suppose we want to transition with $j_1 < j_2 < i_1 < i_2$. Clearly $prefix$ is an array whose increments are monotonically decreasing, so if the $prefix$ part is long enough, $dp$ will also be small enough. That is definitely not optimal.

```cpp
// LOJ6039.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e6 + 200, MAX_K = 5e4 + 200, MAX_C = 330;

typedef long long ll;

int n, m, pos[MAX_N], ptot;
ll dp[MAX_C][MAX_K];
deque<ll> prefix[MAX_C];

void solve(int cid, int cost, int l, int r, int L, int R)
{
    if (l > r || L > R)
        return;
    int mid = (l + r) >> 1, gpt = -1;
    for (int i = L; i <= min(R, mid); i++)
        if ((mid - i <= prefix[cost].size() - 1) && (gpt == -1 || dp[cid - 1][pos[gpt]] + prefix[cost][mid - gpt] < dp[cid - 1][pos[i]] + prefix[cost][mid - i]))
            gpt = i;
    if (l == r)
        return (void)(dp[cid][pos[mid]] = dp[cid - 1][pos[gpt]] + prefix[cost][mid - gpt]);
    solve(cid, cost, l, mid, L, gpt), solve(cid, cost, mid + 1, r, gpt, R);
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1, c, v; i <= n; i++)
        scanf("%d%d", &c, &v), prefix[c].push_back(v);
    for (int i = 1; i < MAX_C; i++)
        if (!prefix[i].empty())
        {
            sort(prefix[i].begin(), prefix[i].end()), reverse(prefix[i].begin(), prefix[i].end());
            for (int j = 1, siz = prefix[i].size(); j < siz; j++)
                prefix[i][j] += prefix[i][j - 1];
            prefix[i].push_front(0);
        }
    int ctot = 0;
    for (int i = 1; i < MAX_C; i++)
        if (!prefix[i].empty())
        {
            ctot++;
            for (int start_pos = 0; start_pos < i; start_pos++)
            {
                ptot = 0;
                for (int p = start_pos; p <= m; p += i)
                    pos[++ptot] = p;
                solve(ctot, i, 1, ptot, 1, ptot);
            }
        }
    for (int i = 1; i <= m; i++)
        printf("%lld ", dp[ctot][i]);
    puts("");
    return 0;
}
```

## Some <span class="slang" data-zh="套路" data-gloss="tàolù, “routine, set pattern”. OI slang for a standard, well-known trick that works on a whole class of problems.">Standard Tricks</span>

### Unrooted Trees

Example problem: [https://kalorona.com/oi/cf-742f/](../2693-cf-742f/index.en.html)

Consider doing the DP first, then fixing the centroid. Of course, you need to apply inclusion–exclusion to the case with two centroids.

There is also a problem that combines this with tree hashing: [https://kalorona.com/oi/bzoj-3162/](../2344-bzoj-3162/index.en.html)

### Sqrt Divide and Conquer

This one is an old chestnut. I remember a knapsack problem on LOJ was done exactly this way. UOJ has a problem like this too.

## <span class="slang" data-zh="背包九讲" data-gloss="Bēibāo Jiǔ Jiǎng, “Nine Lectures on Knapsack”. A classic Chinese tutorial on knapsack-problem DP.">Nine Lectures on Knapsack</span> - Selected Topics
