Editorial for MALD Contest 1 P5 - Scratch Cat and Desktop Backgrounds
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:
Hint 1
How can you find the difference in ones and zeroes in ?
Hint 2
Using a difference array where
1
and 0
, how can you rearrange the formula? How can you deal with the absolute value?
Subtask 1
This problem is solvable in by iterating all substrings of . For each position (end position of substring), decrement all positions (start position of substring) and keep track of ones and zeroes. Alternatively, you can iterate all unique pairs of and and use a prefix sum array. Increment your answer if the current substring has an value between and . Using a -bit floating point (double) won't pass due to precision errors.
Complexity:
Full Solution
Update a difference array where
1
and 0
. After accumulation, now represents in the range .
Step 1. Finding fₗ(K) : Substrings With Imbalance ≤ K (Without Absolute Value>
Let (less) denote the number of substrings with (without absolute value) .
The imbalance of the substring from to is . To make the equation easier to deal with, we will remove the absolute value for now.
We can algebraically manipulate the inequality: For each value (iterate left to right), count the number of positions with a greater or equal value. Inversion counting can be done in using a BIT (Fenwick Tree) or an order statistics tree.
Modifying a tree from the GNU PBDS library allows it to behave like a multiset with order statistic operations. Using a tree from the GNU PBDS library in C++ is much shorter to type but is about ten times slower than a BIT. Subtask compensates those who typed the longer and faster BIT solution.
Step 2. Finding fₛₗ(x) : Substrings With Imbalance < K (Without Absolute Value>
Let (strictly less) denote the number of substrings with (without absolute value) .
We can algebraically manipulate the same inequality from step but with instead of . This results in .
You should now find values strictly greater instead of greater than or equal. You will need to tweak your BIT or ordered statistics tree to accommodate this. When using a BIT, values can reach values like . What you can do is compress values so they fit. Some large constant factor compression implementations might not pass subtask .
Step 3. Finding Substrings With Imbalance < K and ≤ K (With Absolute Value)
Remember that and are calculated using the formula without the absolute value. Because can be negative (more zeroes than ones), you need to count the substrings between and .
Recall (less) is our answer from step : the number of substrings with (without absolute value) .
Recall (strictly less) is our answer from step : the number of substrings with (without absolute value) .
We can modify them to work for absolute values:
- For the number of substrings with (with absolute value), we should be finding the number of in the range :
- For the number of substrings with (with absolute value), we should be finding the number of in the range :
Number of substrings with in range
Number of substrings with in range
Putting Everything Together
Now we have the formulas for and with the absolute value. The number of substrings with in the range is the difference between the number of substrings with and the substrings with .
The final formula for our answer is : the subarrays in the range and .
Final Formula Visualization
A dotted border means exclusive range, while continuous means inclusive range.
Complexity:
Comments