
The initial positions of all the Avengers are sorted in increasing order and no two Avengers have the same initial position.
If an Avenger crosses the other Avenger, they will travel together with Avenger’s speed moving ahead i.e. with the speed of slower Avenger.
The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.
The first line of each test case contains four space-separated integers ‘N’, ‘K’, ‘destination’ and ‘time’ representing the total number of Avengers, required number of Avengers to be saved, the position of time quantum, and the time remaining to destroy the world in hours.
The next line contains ‘N’ single-spaced elements sorted in increasing order, representing Avenger's initial positions.
The next line contains ‘N’ single-spaced elements, representing Avengers speed in km/hour.
For each test case, print the minimum number of swaps required to save at least ‘K’ Avengers. Print -1 if it is impossible to save ‘K’ Avengers.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= K <= N <= 5000
1 <= destination, time <= 10^9
1 <= Speed[i] , Position[i] <= 10^9
Where Position[i] is the initial position of the Avengers and Speed[i] is the initial speed of the Avengers.
Time Limit: 1 sec
The idea here is to traverse the list of Avengers from right to left, and for each Avenger, we will calculate the maximum distance he/she can cover. If this maximum distance is greater than equal to the required distance (distance from the initial position to time quantum), we will increment the count of saved Avengers by 1. Else, we will increase the count of the Avengers which can’t be saved.. If we have encountered an Avenger that can be saved but we have ‘X’ number of Avengers which cannot be saved ahead of him then we will And ‘X’ to our ‘answer’ as we need to bring the Avenger ahead of all Avengers who cannot be saved, for that we need ‘X’ swaps.
Algorithm