Explanation
Let denote the maximum effectiveness of the prefix of length of the unit. The transitions are defined as , where .
Define a prefix sum array . We can then express as . Now evaluate the first equation with our new value of :
We will factor terms that do not depend on outside of the statement.
We will group terms that depend only on with .
To efficiently handle transitions in or , we will use the convex hull optimization.
Time Complexity:
Implementation
C++
#include <bits/stdc++.h>using namespace std;// source:// https://github.com/kth-competitive-programming/kactl/blob/main/content/data-structures/LineContainer.h// supports insertion of linear functions and querying of MAXIMUM value of x for// a given x for details on internal implementation, see LineContainer module// alternatively, learn Li Chao Treeinline namespace _LineContainer {bool _Line_Comp_State;
Alternative Solution
The above solution uses Line Container. Using a deque, it is possible to solve this problem in time although this is not necessary. See the Convex Hull Trick for more details.
This section is not complete.
Any help would be appreciated! Just submit a Pull Request on GitHub.
- Linear time solution using deque and convex hull trick
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!