Skip to content

Commit 3c11658

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent d79e3ea commit 3c11658

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

content/6_Advanced/Lagrange.mdx

+24-25
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Here is where the fact that $f(x)$ is concave comes in. Because the slope is non
103103

104104
Let $v(\lambda)$ be the optimal maximal achievable sum with $\lambda$ penalty and $c(\lambda)$ be the number of subarrays used to achieve $v(\lambda)$ (note that if there are multiple such possibilities, we set $c(\lambda)$ to be the **maximal** number of subarrays to achieve $v(\lambda)$). These values can be calculated in $\mathcal{O}(N)$ time using the dynamic programming approach described above.
105105

106-
When we assign the penalty of $\lambda$, we are trying to find the maximal sum if creating a subarray reduces our sum by $\lambda$. So $v(\lambda)$ will be the **maximum** of $f(x) - \lambda x$ and $c(\lambda)$ will equal to the rightmost $x$ that **maximizes** $f(x) - \lambda x$.
106+
When we assign the penalty of $\lambda$, we are trying to find the maximal sum if creating a subarray reduces our sum by $\lambda$. So $v(\lambda)$ will be the **maximum** of $f(x) - \lambda x$ and $c(\lambda)$ will equal to the rightmost $x$ that **maximizes** $f(x) - \lambda x$.
107107

108108
Given the shape of $f(x) - \lambda x$, we know that $f(x) - \lambda x$ will be maximized at all points where $\lambda$ is equal to the slope of $f(x)$ (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 $\lambda$. So this means that $c(\lambda)$ will be the rightmost $x$ at which the slope of $f(x)$ is still greater or equal to $\lambda$.
109109

@@ -123,36 +123,35 @@ using namespace std;
123123
const int MAX = 300000;
124124
const ll INF = (ll)300000 * 1000000000;
125125

126-
int n, k, A[MAX + 1];
126+
int n, k, A[MAX + 1];
127127
pair<ll, ll> dp[MAX + 1][2];
128-
129-
pair<ll, ll> solveLambda(ll lmb){
130-
dp[0][0] = {0, 0};
131-
dp[0][1] = {-INF, 0};
132-
133-
for (int i = 1; i <= n; i++){
134-
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
135-
136-
dp[i][1] = max(
137-
make_pair(dp[i - 1][0].first + A[i] - lmb, dp[i - 1][0].second + 1),
138-
make_pair(dp[i - 1][1].first + A[i], dp[i - 1][1].second)
139-
);
140-
}
141-
return max(dp[n][0], dp[n][1]);
128+
129+
pair<ll, ll> solveLambda(ll lmb) {
130+
dp[0][0] = {0, 0};
131+
dp[0][1] = {-INF, 0};
132+
133+
for (int i = 1; i <= n; i++) {
134+
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
135+
136+
dp[i][1] = max(
137+
make_pair(dp[i - 1][0].first + A[i] - lmb, dp[i - 1][0].second + 1),
138+
make_pair(dp[i - 1][1].first + A[i], dp[i - 1][1].second));
139+
}
140+
return max(dp[n][0], dp[n][1]);
142141
}
143142

144-
int main(){
145-
cin >> n >> k;
143+
int main() {
144+
cin >> n >> k;
146145

147-
for (int i = 1; i <= n; i++) cin >> A[i];
146+
for (int i = 1; i <= n; i++) cin >> A[i];
148147

149-
ll lo = 0, hi = INF;
148+
ll lo = 0, hi = INF;
150149

151-
while (lo < hi){
152-
ll M = (lo + hi) / 2;
153-
solveLambda(M).second <= k ? hi = M : lo = M + 1;
154-
}
155-
cout << solveLambda(lo).first + lo * k << endl;
150+
while (lo < hi) {
151+
ll M = (lo + hi) / 2;
152+
solveLambda(M).second <= k ? hi = M : lo = M + 1;
153+
}
154+
cout << solveLambda(lo).first + lo * k << endl;
156155
}
157156
```
158157

0 commit comments

Comments
 (0)