‘ORDER_ARR[i] = [RATE[i], AMOUNT[i], TYPE[i]]’ denotes that ‘AMOUNT’ orders have been placed of type ‘TYPE’ at the rate ‘RATE’. The ‘TYPE’ is:
0 represents it as a batch of buy orders, or
1 represents it as a batch of sell orders.
If the order is a buy order, you look at the sell order with the smallest price in the ‘KHATA’.
If that sell order's price is smaller than or equal to the current buy order's price, they will match and be executed, and that sell order will be removed from the ‘KHATA’.
Else, the buy order is added to the KHATA’.
If the order is a sell order, you look at the buy order with the largest price in the ‘KHATA’.
If that buy order's price is larger than or equal to the current sell order's price, they will match and be executed, and that buy order will be removed from the ‘KHATA’.
Else, the sell order is added to the ‘KHATA’.
Suppose the given ‘ORDER_ARR’ is [ [ 10, 5, 0 ], [ 15, 2, 1 ], [ 25, 1, 1 ], [ 30, 4, 0 ] ]
Now 5 orders of type buy with price 10 are placed. There are no sell orders, so the 5 orders are added to the ‘KHATA’.
2 orders of type sell with price 15 are placed. There are no buy orders with prices larger than or equal to 15, so the 2 orders are added to the ‘KHATA’.
1 order of type sell with price 25 is placed. There are no buy orders with prices larger than or equal to 25 in the ‘KHATA’, so this order is added to the ‘KHATA’.
4 orders of type buy with price 30 are placed. The first 2 orders are matched with the 2 sell orders of the least price, which is 15 and these 2 sell orders are removed from the ‘KHATA’. The 3rd order is matched with the sell order of the least price, which is 25 and this sell order is removed from the ‘KHATA’. Then, there are no more sell orders in the ‘KHATA’, so the 4th order is added to the ‘KHATA’.
Finally, the ‘KHATA’ has 5 buy orders with a price of 10, and 1 buy order with a price of 30. So the total number of orders in the ‘khata’ is 6.
1. The answer can be large, so return (ans % 10^9+7)
The first line of input contains a ‘T’ number of test cases.
The first line of each test case contains an integer ‘N’ denoting the number of rows in array ‘ORDER_ARR’. Then, ‘N’ lines follow.
Each line contains three integers denoting the number of columns in that row and ‘3’ space-separated integers denoting the ‘RATE’, ‘AMOUNT’, and ‘TYPE’ value respectively.
For each test case, print the total amounts of order in the ‘KHATA’.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 1000
1 <= ‘RATE’, ‘AMOUNT’ <=10 ^ 6
‘TYPE’ = [ ‘0’, ‘1’ ]
Where ‘T’ represents the number of test cases and ‘N’ represents the size of ‘ORDER_ARR’.
Time Limit: 1 sec.
Approach: The idea here is to traverse the ‘ORDER_ARR’ and if an order is of type ‘BUY’ we must check our ‘KHATA’ for sell order and find the minimum value order from that using ‘PRIORITY QUEUE’ and compare it. If the order is of type ‘sell’ we have to check our ‘KHATA’ for buy order and find the maximum value using min heap order from that and then compare it. In this way, we traverse our ‘ORDER_ARR’.
Algorithm:
Split String
Ninja And The Class Room
Equal Arrays
Ninja And The Strictly Increasing Array
Maximize