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. every time 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 (assuming is at most the number of positive elements). 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 the sum 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 maximal 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 . So will be the maximum of and will equal to the rightmost that maximizes .
Given the shape of , we know that will be maximized at all points where is equal to the slope of (these points are red in the graph above). If there are no such points it will be maximized at the rightmost point where the slope is less than . So this means that will be the rightmost at which the slope of is still greater or equal to .
Now we know exactly what represents: is the slope and is the rightmost at which the slope of is still greater or equal to .
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 longint main() {int n, k;cin >> n >> k;int a[n];
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!