Lagrangian Relaxation
Authors: Benjamin Qi, Alex Liang, Dong Liu
aka Aliens Trick
Prerequisites
Resources
Resources | ||||
---|---|---|---|---|
Mamnoon Siam | ||||
Serbanology |
Lagrangian Relaxation
Lagrangian Relaxation involves transforming a constraint on a variable into a cost and binary searching for the optimal .
Focus Problem – try your best to solve this problem before continuing!
View Internal SolutionThe problem gives us a length () array of integers in the range . We are given some () and are asked to choose at most disjoint subarrays such that the sum of elements included in a subarray is maximized.
Intuition
The main bottleneck of any dynamic programming solution to this problem is having to store the number of subarrays we have created so far.
Let's try to find a way around this. Instead of storing the number of subarrays we have created so far, we assign a penalty of for creating a new subarray (i.e. everytime we create a subarray we penalize our sum by ).
This leads us to the sub-problem of finding the maximal sum and number of subarrays used if creating a new subarray costs . We can solve this in time with dynamic programming.
Dynamic Programming Solution
Let be the maximal achievable sum with penalty and be the number of subarrays used to achieve . Then the maximal possible sum achievable if we use exactly subarrays is . Note that we add to undo the penalty.
Our goal is to find some such that . As we increase , it makes sense for to decrease since we are penalizing subarrays more. Thus, we can try to binary search for to make and set our answer to be at the optimal .
This idea almost works but there are still some very important caveats and conditions that we have not considered.
Geometry
Let be the maximal sum if we use at most subarrays. We want to find .
The first condition is that must be concave or convex. Since is increasing in this problem, the means that we need to be concave: . In other words, this means that the more subarrays we add, the less we increase our answer by. We can intuitively see that this is true.
Proof that our function is concave
Consider the following graphs of and . In this example, we have .
Here is where the fact that is concave comes in. Because the slope is non-increasing, we know that will first increase, then stay the same, and finally decrease.
Let be the optimal maximal achievable sum with penalty and be the number of subarrays used to achieve (note that if there are multiple such possibilities, we set to be the minimal number of subarrays to achieve ). These values can be calculated in time using the dynamic programming approach described above.
When we assign the penalty of , we are trying to find the maximal sum if creating a subarray reduces our sum by . In other words, we are trying to find the maximum of .
Without loss of generality, suppose there exists a slope equal to . Given the shape of , we know that will be maximized at the points where is equal to the slope of (these points are red in the graph above). This means that will be the point at which is equal to the slope of (if there are multiple such points, then will be the leftmost one).
Now we know exactly what represents: is the slope and is the position with slope equal to (if there are multiple such positions then is the leftmost one).
We binary search for and find the highest such that . Let the optimal value be . Then our answer is . Note that this works even if since and will be on the same line with slope in that case.
Because calculating and with the dynamic programming solution described above will take time, this solution runs in time.
#include <bits/stdc++.h>using namespace std;#define ll long long#define pll pair<ll, ll>#define f first#define s secondconst int MAX = 3e5 + 5;
Problems
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
Platinum | Easy | ||||
CF | Normal | ||||
CF | Normal | ||||
FHC | Normal | ||||
Kattis | Normal | ||||
Balkan OI | Normal | ||||
IOI | Hard |
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!