P3628: [APIO2010] Commando — Solution & Slope Optimization Analysis
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)\), GG.
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
// 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;
}