D — Almost Triple Deletions
Author: Gheal
Hints
Hint 1
Consider the opposite problem: What is the smallest possible length of a final array?
Hint 2
For which arrays is the smallest possible final length equal to
Unable to parse markup [type=CF_MATHJAX]
$?
Hint 3
Considering the second hint, it is possible to completely remove some subsequences from the array.
Solution
Lemma: An array $$$a_1,a_2,\ldots, a_n$$$ can be fully deleted via a sequence of operations if and only if it satisfies both of the following constraints:
Unable to parse markup [type=CF_MATHJAX]
$ is even
The maximum frequency of any element in the array is at most $$$\frac n2$$$.
Proof
If
Unable to parse markup [type=CF_MATHJAX]
$ is odd, then any final array will also have an odd length, which can't be
Unable to parse markup [type=CF_MATHJAX]
$.
An optimal strategy is to always delete one of the most frequent elements and any one of its neighbours. If the most frequent element occurs
Unable to parse markup [type=CF_MATHJAX]
$ times, then the final array will have at least
Unable to parse markup [type=CF_MATHJAX]
$ elements. Otherwise, this strategy ensures the full deletion of the array, since, after performing an operation, it is impossible for an element to occur more than
Unable to parse markup [type=CF_MATHJAX]
$ times in the array.
Since the maximum frequency of a value for every subsequence
Unable to parse markup [type=CF_MATHJAX]
$ can be computed in
Unable to parse markup [type=CF_MATHJAX]
$, it is possible to precompute all subsequences which can be deleted via a sequence of operations.
Let
Unable to parse markup [type=CF_MATHJAX]
$ be the maximum length of a final array consisting of
Unable to parse markup [type=CF_MATHJAX]
$ and some subarray from the first
Unable to parse markup [type=CF_MATHJAX]
$ elements. Initially,
Unable to parse markup [type=CF_MATHJAX]
$ is set to
Unable to parse markup [type=CF_MATHJAX]
$ if the prefix
Unable to parse markup [type=CF_MATHJAX]
$ can be fully deleted. Otherwise,
Unable to parse markup [type=CF_MATHJAX]
$.
For every pair of indices
Unable to parse markup [type=CF_MATHJAX]
$ and
Unable to parse markup [type=CF_MATHJAX]
$ (
Unable to parse markup [type=CF_MATHJAX]
$), if we can fully delete the subsequence
Unable to parse markup [type=CF_MATHJAX]
$, then we can append
Unable to parse markup [type=CF_MATHJAX]
$ to any final array ending in
Unable to parse markup [type=CF_MATHJAX]
$. Naturally,
Unable to parse markup [type=CF_MATHJAX]
$ will be strictly greater than
Unable to parse markup [type=CF_MATHJAX]
$. This gives us the following recurrence:
Unable to parse markup [type=CF_MATHJAX]
$
If we define a final array as a subarray of equal elements from the array
Unable to parse markup [type=CF_MATHJAX]
$, to which
Unable to parse markup [type=CF_MATHJAX]
$ is forcefully appended, then the final answer can be written as
Unable to parse markup [type=CF_MATHJAX]
$. Note that, when computing
Unable to parse markup [type=CF_MATHJAX]
$,
Unable to parse markup [type=CF_MATHJAX]
$ should not be compared to
Unable to parse markup [type=CF_MATHJAX]
$.
Total time complexity per testcase:
Unable to parse markup [type=CF_MATHJAX]
$.