Editorial for Max's Anger Contest Series 1 P5 - Slacking Subsequences
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Realize a DP state:
: the minimum sum of absolute differences for the
integer in the array with a subsequence of length
.
Then, for each given state, consider choosing any integer in the array before the one:
Thus, , where
and
represents the absolute difference between
and
.
Time Complexity:
Subtask 2
Realize that the transition can be optimized with Fenwick trees or
segment trees:
- Maintain
for all integers that come before
but have
with
at the
index.
- Maintain
for all integers that come before
but have
with
at the
index.
Then, for each given state, , where
returns the minimum
for all indices less than or equal to
and
returns the minimum
for all indices greater than
.
Time Complexity:
Comments