Zero Subarray Problem
Limits: 2 sec., 512 MiB
This statement was translated automatically from Ukrainian.
Given three integers \(n\), \(m\), and \(k\). Also, an array \(a\) of length \(n\) is given.
You can perform two types of operations:
Choose two indices \(i\) and \(j\) (\(i \neq j\)) and swap the elements \(a_i\) and \(a_j\). This operation can be performed at most \(k\) times.
Decrease any array element by \(1\), if it is greater than \(0\). This operation can be performed any number of times.
Zenyk performs these operations in any order and wants to ensure that after all operations there is a subarray of length \(m\) consisting entirely of zeros.
Input
The first line contains three integers \(n\), \(m\), and \(k\) — the length of the array, the length of the subarray that Zenyk wants to zero out, and the maximum number of operations of the first type.
The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) — the elements of the array.
Output
Output a single number — the minimum number of operations of the second type required to form a subarray where all elements are equal to zero.
Constraints
\(1 \le m \le n \le 4 \cdot 10^5\),
\(1 \le k \le 4 \cdot 10^5\),
\(1 \le a_i \le 10^9\).
Scoring consists of the following blocks:
1 point for each example from the statement,
8 points: \(n \le 2000, k = 1\),
14 points: \(m \le k\),
17 points: \(n \le 10^5, k \le 10\),
24 points: \(n, k \le 10^5\),
35 points: no additional constraints.
You will receive points for a block only if your program passes all tests in that block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 3 2 1 2 5 2 4 4 6 | 5 |
| Input (stdin) | Output (stdout) |
|---|---|
| 5 2 1 1 4 4 6 2 | 3 |
Notes
In the first example:
We use the operation of the first type and swap the elements at indices \(3\) and \(4\).
After that, we take the subarray of length \(3\) from the first three elements, it looks like this: [1, 2, 2]
To make all its elements equal to zero, we use operations of the second type. This requires 5 operations.
This is the optimal answer for this example.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Zero Subarray Problem - Editorial
Clearly, the elements we put into the window of length \(m\) determine the total number of operations, since each of them has to be zeroed.
Suppose we first sort all elements of \(a\) in increasing order. We call the \(m\) smallest elements good, because they are cheaper to zero, and all the others bad.
Intuitively, we want the subarray of length \(m\) to contain as many good elements as possible, but we can swap at most \(k\) of them. This means that at most \(k\) smallest elements from outside the window can be brought into the window, throwing out the \(k\) largest elements currently in it.
For each window of length \(m\), let \[sm = \sum_{i \in \text{window}} a_i\] be the sum of the elements in the current window.
Let \(in\) be the set of the \(k\) largest bad elements inside the window (the ones it pays most to replace). Let \(out\) be the set of the \(k\) smallest good elements outside the window (the ones it pays most to bring in).
Then the minimum cost for this window is: \[\text{cost} = sm - \sum_{x \in in} x + \sum_{y \in out} y\]
that is, we subtract the \(k\) largest values in the window and add the \(k\) smallest outside it, modelling the \(k\) possible swaps. The answer is the minimum \(cost\) over all windows.
To maintain the sum of the \(k\) smallest elements efficiently, we use two multisets (multiset in C++):
the first multiset stores the \(k\) smallest elements;
the second one stores the rest.
When an element is added or removed, we rebalance them so that the first multiset always contains exactly the \(k\) smallest elements. The \(k\) largest can be maintained the same way by multiplying the values by -1.
The complexity is \(O(n \cdot log_n)\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|