Balancing Weapons
Contest: Educational Codeforces Round 146 (Rated for Div. 2)
Difficulty rating: 2500
Tags: binary search, brute force, data structures, math, two pointers

You've got a job in a game studio that developed an online shooter, and your first big task is to help to balance weapons. The game has $$$n$$$ weapons: the $$$i$$$-th gun has an integer fire rate $$$f_i$$$ and an integer damage per bullet $$$d_i$$$. The $$$i$$$-th gun's total firepower is equal to $$$p_i = f_i \cdot d_i$$$.

You have to modify the values $$$d_i$$$ of some guns in such a way that the new values $$$d_i$$$ will still be integers, and the firepower of all guns will become balanced. Given an integer $$$k$$$, the guns are said to be balanced if $$$\max\limits_{1 \le i \le n}{p_i} - \min\limits_{1 \le i \le n}{p_i} \le k$$$.

Since gamers that play your game don't like big changes, you need to change the values $$$d_i$$$ for the minimum possible number of guns. What is the minimum number of guns for which you have to change these values to make the guns balanced?

Note that the new values $$$d_i$$$ must be integers greater than $$$0$$$.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 3000$$$; $$$0 \le k \le 1500$$$) — the number of guns to balance, and the maximum allowed gap between the most and the least powerful weapons.

The second line contains $$$n$$$ integers $$$f_1, f_2, \dots, f_n$$$ ($$$1 \le f_i \le 2000$$$), where $$$f_i$$$ is the fire rate of the $$$i$$$-th gun.

The third line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$1 \le d_i \le 10^9$$$), where $$$d_i$$$ is the damage per bullet of the $$$i$$$-th gun.

It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3000$$$.

Output format:
For each test case, print the minimum number of guns which damage $$$d_i$$$ you have to change in order to make the guns balanced.

Note that the new values $$$d_i$$$ must be integers greater than $$$0$$$.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first test case, you can set $$$d_1 = 2$$$ and $$$d_2 = 4$$$. You'll get an array $$$d = [2, 4, 1, 2]$$$, and the values of firepower will be $$$p = [12, 12, 13, 14]$$$. The guns are balanced, since $$$14 - 12 \le 2$$$.

In the second test case, you have to change the value $$$d_i$$$ for all three guns. For example, you can set $$$d = [5151, 5100, 5050]$$$.

In the third test case, all guns are already balanced, so you don't have to change anything.

Example input:
5
4 2
6 3 13 7
1 2 1 2
3 2
100 101 102
100 99 98
5 0
1 12 4 4 3
12 1 3 3 4
2 50
1000 10
1000000000 1
3 5
1 19 11
49 4 72

Example output:
2
3
0
1
2

Editorial:
Note that the answer $$$n$$$ is always possible: for example, we can set $$$d_i = \frac{\prod{f_j}}{f_i}$$$, then $$$p_1 = \dots = p_n = \prod{f_j}$$$ and $$$\max{p_i} - \min{p_i} = 0$$$.
If the answer is less than $$$n$$$ then there is at least one gun $$$id$$$ we won't change. It means that all other guns' firepower should be "around" $$$p_{id}$$$, i. e. $$$|p_i - p_{id}| \le k$$$. So we can look at segment $$$[p_{id} - k, p_{id} + k]$$$ and, for each gun $$$i$$$, find what values $$$d'_i$$$ we should set to get into this segment. After that we can rephrase our task into the next one: we should choose segment $$$[l, l + k] \subset [p_{id} - k, p_{id} + k]$$$ such that each gun occurs in $$$[l, l + k]$$$ at least once and the number of corresponding $$$d'_i$$$ that are equal to $$$d_i$$$ is maximum possible.
It can be solved with two pointers technique. Note that there are at most three interesting values $$$d'_i$$$ we should consider: $$$v_1 = \left\lfloor \frac{p_{id}}{f_i} \right\rfloor$$$, $$$v_2 = v_1 + 1$$$ and $$$v_3 = d_i$$$. For each unique value $$$v_j$$$ such that $$$v_j \cdot f_i \in [p_{id} - k, p_{id} + k]$$$ we can add an event $$$(i, c_j)$$$ in position $$$v_j f_i$$$, where $$$c_j$$$ is $$$1$$$ if $$$v_j = d_i$$$ or $$$0$$$ otherwise.
Now, with two pointers technique, we can iterate over all subsegments of length $$$k + 1$$$ of segment $$$[p_{id} - k, p_{id} + k]$$$. To get the desired answer we should maintain the number of unique $$$i$$$ from events that are present in the subsegment and the sum $$$s$$$ of $$$c_j$$$ from that events. Since there is only one $$$c_j = 1$$$ for each gun $$$i$$$ then the sum $$$s$$$ of $$$c_j$$$ we have is equal exactly to the number of guns we shouldn't change. Then we take the maximum $$$mx$$$ over sums $$$s$$$ of all subsegments where all guns occur, and the answer for a fixed $$$p_{id}$$$ is $$$n - mx$$$.
Let's iterate over all "fixed" $$$id$$$ and take the minimum from all $$$n - mx$$$: that will be the answer for the initial task.
Checking answer for a fixed $$$id$$$ involves creating $$$3 n$$$ events and two pointers over segment $$$[p_{id} - k, p_{id} + k]$$$, so it takes $$$O(n + k)$$$ time and $$$O(n + k)$$$ space. So, the total complexity is $$$O(n^2 + n k)$$$ time and $$$O(n + k)$$$ space.