Let dp[i] denote the maximum effectiveness of the prefix of length i of the unit. The transitions are defined as dp[i]=max0≤j<i{dp[j]+ax2+bx+c}, where x=sum(j+1,i)=∑k=j+1ixk.
Define a prefix sum array pre[i]=∑k=1ixk. We can then express x as pre[i]−pre[j]. Now evaluate the first equation with our new value of x:
// 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 Tree
inlinenamespace _LineContainer {
bool _Line_Comp_State;
Alternative Solution
The above solution uses Line Container. Using a deque, it is possible to solve this problem in O(n) 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!