---
id: 898
lang: en
translation_of: index.md
source_sha256: bed21ac19e9a8744811530bbdf6380178bfb40a40a71372bf09928b62a1ec561
title: "P3628: [APIO2010] Commando — Solution & Slope Optimization Analysis"
description: "The O(n^2) DP for APIO2010 Commando, then slope optimization step by step: the slope inequality, popping the queue head, and maintaining the tail."
tags: ["OI","DP","DP Optimization","Convex Hull Trick"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "特别行动队 rendered as “Commando”, the official English name of the APIO 2010 problem."
  - "斜率优化 in the title and prose rendered as “slope optimization” to match the post's 斜率方程 / 斜率不等式 (slope equation / slope inequality) wording; the tag uses the tags.yaml form “Convex Hull Trick”."
---

## Brute Force Approach

Let’s first work out the ordinary DP equation for this problem. Obviously, after maintaining prefix sums, at $f[i]$ we just enumerate a point $j$ to split at:

$$
f[i] = max_{j\in [1,i-1]} \{f[j]+a(S[i]-S[j])^2+b(S[i]-S[j])+c\}
$$

The time complexity is $O(n^2)$, <span class="slang" data-zh="GG" data-gloss="Gamer shorthand for “good game”, typed when conceding a match. Here: it is over, I am done for.">GG</span>.

## Slope Optimization Analysis

### Step 1 - Write Out the Slope Equation

This is the first step in analyzing every slope optimization problem: given two values $j_1<j_2$, write out the slope inequality to get a monotonic relation. In this step you just separate the parameter from the variable, which is a little bit tedious.

Taking this problem as an example, let’s derive the slope equation:

$$
\begin{gathered} f[j_1]+a(S[i]-S[j_1])^2+b(S[i]-S[j_1])+c>f[j_2]+a(S[i]-S[j_2])^2+b(S[i]-S[j_2])+c \\ (f[j_1]-f[j_2])+a(S[j_1]^2-S[j_2]^2)+b(S[j_2]-S[j_1])>2a(S[j_1]-S[j_2])*S[i] \\ \frac{(f[j_1]-f[j_2])+a(S[j_1]^2-S[j_2]^2)+b(S[j_2]-S[j_1])}{2a(S[j_1]-*S[j_2])}>S[i] \end{gathered}
$$

**Note:** $a<0$**.**

### Step 2 - The Condition for Popping the Queue Head

Looking at this equation, when $j_2$ is better than $j_1$:

$$
\frac{(f[j_1]-f[j_2])+a(S[j_1]^2-S[j_2]^2)+b(S[j_2]-S[j_1])}{2a(S[j_1]-S[j_2])} \leq S[i]
$$

Since $S[i]$ is monotonically non-decreasing as $i$ grows, once $j_2$ is better than $j_1$, $j_1$ will never be considered again, so pop it.

### Step 3 - Transition from the Queue Head

Nothing much to say about this one: just use $q[head]$ directly as $j$.

### Step 4 - Handling the Queue Tail

We also need to guarantee the monotonicity of the slopes of these lines. First, taking this problem as an example, let’s analyze the monotonicity of the slopes.

Suppose the slope function is $slope(j_1,j_2)$; its expression is exactly the formula we derived above, namely:

$$
slope(j_1, j_2) = \frac{(f[j_1]-f[j_2])+a(S[j_1]^2-S[j_2]^2)+b(S[j_2]-S[j_1])}{2a(S[j_1]-S[j_2])}
$$

Suppose the slopes of the lines must be monotonically increasing, that is, $l<mid<r, slope(l,mid)<slope(mid,r)$. In the monotonic queue we only keep the options that could become the optimal solution, that is, for elements $a_1,a_2,a_3$, $a_1$ is better than $a_2$ and $a_2$ is better than $a_3$. So, in between, we have:

$$
slope(l,mid)>S[i] \text{ and } slope(mid,r)<S[i]
$$

Rearranging gives:

$$
slope(l,mid)>slope(mid,r)
$$

This is the opposite of the assumption. So when we handle the queue tail, we are really maintaining this relation, so the pop condition is “if this slope is monotonically increasing”:

$$
l<mid<r, slope(l,mid)<slope(mid,r)
$$

So, the analysis is complete.

## Code

```cpp
// P3628.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 1e6 + 2000;
ll n, xi[MAX_N], a, b, c, S[MAX_N], f[MAX_N], q[MAX_N], head, tail;
ll pow(ll num) { return num * num; }
double slope(int j1, int j2)
{
    return double((f[j1] - f[j2] + b * (S[j2] - S[j1]) + a * (pow(S[j1]) - pow(S[j2])))) / double(2 * a * (S[j1] - S[j2]));
}
int main()
{
    scanf("%lld%lld%lld%lld", &n, &a, &b, &c);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &xi[i]), S[i] = S[i - 1] + xi[i];
    head = 1, tail = 1;
    for (int i = 1; i <= n; i++)
    {
        while (head < tail && slope(q[head], q[head + 1]) <= S[i] * 1.0)
            head++;
        f[i] = f[q[head]] + a * pow(S[i] - S[q[head]]) + b * (S[i] - S[q[head]]) + c;
        while (head <= tail && slope(q[tail - 1], q[tail]) >= slope(q[tail], i))
            tail--;
        q[++tail] = i;
    }
    printf("%lld", f[n]);
    return 0;
}
```
