Skip to content

Commit 14e06c8

Browse files
authored
Update Lagrange.mdx
1 parent cdb0e0b commit 14e06c8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

content/6_Advanced/Lagrange.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Given the shape of $f(x) - \lambda x$, we know that $f(x) - \lambda x$ will be m
110110

111111
Now we know exactly what $\lambda$ represents: $\lambda$ is the slope and $c(\lambda)$ is the rightmost $x$ at which the slope of $f(x)$ is still greater or equal to $\lambda$.
112112

113-
We binary search for $\lambda$ and find the first $\lambda$ such that $c(\lambda) \ge K$. Let the optimal value be $\lambda_{\texttt{opt}}$. Then our answer is $v(\lambda_{\texttt{opt}}) + \lambda_{\texttt{opt}} K$. Note that this works even if $c(\lambda_{\texttt{opt}}) \neq K$ since $c(\lambda_{\texttt{opt}})$ and $K$ will be on the same line with slope $\lambda_{\texttt{opt}}$ in that case.
113+
We binary search for $\lambda$ and find the highest $\lambda$ such that $c(\lambda) \ge K$. Let the optimal value be $\lambda_{\texttt{opt}}$. Then our answer is $v(\lambda_{\texttt{opt}}) + \lambda_{\texttt{opt}} K$. Note that this works even if $c(\lambda_{\texttt{opt}}) \neq K$ since $c(\lambda_{\texttt{opt}})$ and $K$ will be on the same line with slope $\lambda_{\texttt{opt}}$ in that case.
114114

115115
Because calculating $v(\lambda)$ and $c(\lambda)$ with the dynamic programming solution described above will take $\mathcal{O}(N)$ time, this solution runs in $\mathcal{O}(N\log{\sum A[i]})$ time.
116116

@@ -153,8 +153,8 @@ int main() {
153153
ll lo = 0;
154154
ll hi = 1e18;
155155
while (lo < hi) {
156-
ll mid = (lo + hi) / 2;
157-
solve_lambda(mid).second <= k ? hi = mid : lo = mid + 1;
156+
ll mid = (lo + hi + 1) / 2;
157+
solve_lambda(mid).second >= k ? lo = mid : hi = mid - 1;
158158
}
159159

160160
cout << solve_lambda(lo).first + lo * k << endl;

0 commit comments

Comments
 (0)