Consider a pipe of length L. The pipe has N water droplets at N different positions within it. Each water droplet is moving towards the end of the pipe (x = L) at different speeds. When a water droplet mixes with another water droplet, it assumes the speed of the water droplet it is mixing with, or to put it in other words, the water resulting water droplet formed post mixing will take the speed of the smaller ones.
Determine the number of droplets that come out of the pipe.
The first line contains an integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case or query contains two integers(N and L) separated by a single space. Where 'N' represents the number of water droplets present in the pipe initially and 'L' represents the length of the pipe.
The second line contains 'N' single space-separated integers representing the positions of the droplets initially.
The third line contains 'N' single space-separated integers representing the velocities of the droplets initially.
Output Format :
For each test case, return the number of water droplets that come out of the pipe.
Output for every test case will be printed in a separate line.
Note:
The positions of all the droplets are unique in that is no water droplet will have the same initial position.
You do not need to print anything, it has already been taken care of.
1 <= t <= 100
0 <= N <= 10^5
1 <= L <= 10^9
0 <= Position[i] <= L
1 <= Speed[i] <= 10^9
Time Limit: 1 sec
1
5 12
10 8 0 5 3
2 4 1 1 3
3
1
2 5
0 1
1 1
2
Can we optimize search for the first two minimum positions?
We can solve this problem by calculating the time required by each drop to reach the end of the pipe and comparing them based on their position in the pipe and the time required to reach the end.
We can observe all the drops before a slower drop will mix with it. And all the drops after the slower drop mix with the next slower drop and so on.
For example, if the times to reach the end are - 12, 3, 7, 8, 1 (sorted according to positions)
O(N * log(N)), Where N is the total number of droplets.
N operations to make a list of pairs where each pair contains the position of every droplet and its respective time to reach the destination.
Then N * log(N) operations to sort the list of pairs on the basis of positions.
N operations to figure out which all drops mix together, and which all don't. We used a stack for this.
O(N), Where N is the total number of droplets.
As we have a list of pairs of size N where each pair contains the position of every droplet and its respective time to reach the destination.