HIMARS
Limits: 3 sec., 256 MiB
This statement was translated automatically from Ukrainian.
In the BNR, there is a convoy of \(n\) Russian military objects. The convoy can be represented as a ray starting at the border of the BNR and Ukraine. Military object \(i\) is located at a distance \(a_i\) from the start of the convoy.
Zenik has two types of weaponry: Grad and HIMARS. Grad is able to destroy all Russian military objects on a single subsegment of the convoy of length \(x\) inclusive of its endpoints, and HIMARS — on a subsegment of length \(y\) inclusive of its endpoints. The number of Grad shells is unlimited, but HIMARS can only be fired \(k\) times.
What is the minimum number of shots required to destroy all Russian military objects?
Input
The first line contains 4 integers \(n\), \(k\), \(x\), and \(y\) — the number of Russian military objects, the number of available HIMARS shots, and the lengths of the segments covered by Grad and HIMARS, respectively.
The next line contains \(n\) integers \(a_i\) — the distances at which the military objects are located from the start of the convoy.
Output
Output a single number — the minimum number of shots required to destroy all Russian military objects.
Constraints
\(1 \le n \le 10^4\),
\(1 \le x, y \le 10^{9}\),
5 points:
\(1 \le k \le 10\),
\(1 \le a_i \le 1000\),
20 points:
\(1 \le k \le 2000\),
\(1 \le a_i \le 10^{9}\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 4 1 1 3 7 4 47 74 | 3 |
Notes
In the first test, you need to fire a HIMARS shot at the first two military objects simultaneously and use one Grad shot each for the third and fourth military objects.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
HIMARS - Editorial
First of all, sort the military targets by their distance from the start of the column. Since the targets do not move, there is no point in shelling the same area more than once.
We use dynamic programming. We destroy the targets from left to right. A state is described by the number \(cnt\) of military targets destroyed from the start of the column. Since the number of HIMARS shots is limited, this must also be part of the state. So our DP is \(dp[cnt][himars]\): the minimum number of shots to destroy the first \(cnt\) targets making exactly \(himars\) HIMARS shots. To compute it, we assume that the right end of the last shot is exactly at the position of the \(cnt\)-th target. All targets with coordinates in \([a_{cnt} - x, a_{cnt}]\) are destroyed by a Grad, and in \([a_{cnt} - y, a_{cnt}]\) by a HIMARS. So for each kind of shot we need to find the leftmost target it hits. This can be done with binary search or with two pointers, since it does not depend on \(himars\). Then we know how many targets remain on the left, and we know the DP value for them, that is, the smallest number of shots to destroy this prefix. Clearly, a HIMARS shot can be made only if \(himars > 0\), and then the DP value is the minimum of the two kinds of shots.
To find the answer, we go over \(himars\), the number of HIMARS shots we make. The answer is the minimum of the values \(dp[n][himars]\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|