I dont want the whole solution, just a hint or intuition, I am trying to think of a solution but none of the approaches I have come up with so far are giving the minimum possible answer for all test cases
I am unable to think of an optimal solution, can i get a hint on how to approach this problem
Think greedy, at any instance
from point a you will send the two with min cost and return the min cost from b-a, also keep a check of those who are at b
suppose
1 2 3 4 are four people, sort it
send 2 with min cost let’s say 2 and 4
return 2 since it is min cost
now send 1 and 3 and send 4(curr min cost at b) back and now resend both
repeat the process until all are not at point b
@seemantanishth i came up with an algo, but which Data Structures should I use to efficiently implement this algorithm?
Algo:
- From A, pick the pair with minimum difference. If there are 2 such pairs then pick the pair with the minimum value. Send this pair to B and add cost of max(val1, val2)
- From B, pick the minimum element in B and sent it to A. Add cost of that element.
- Continue step1 and step 2 until only 2 elements are remaining in A.
- Send the 2 elements to B and add the cost of max(val1, val2)
vector<vector<pair<int,int>>>