Student Rearrangement Problem Problem Statement In a school, two lines of students, A…
- User
Student Rearrangement Problem Problem Statement In a school, two lines of students, A and B, are arranged with N students in each line, facing each other. The Physical Education teacher aims to make the heights of students standing across from each other equal. This can be achieved by swapping students between the two lines. Any student in one line can be swapped with any student in the other line. Each swap, regardless of the students involved, incurs a cost equal to the height of the shorter student in the swap. Rearranging students within the same line does not incur any cost. The goal is to calculate the minimum cost required to attain this arrangement where students directly facing each other have the same height. If it’s not feasible to achieve this arrangement, the result should be -1. Function Description Complete the function rearrangeStudents with the following parameters: • vector arrA: the heights of children in line A • vector arrB: the heights of children in line B Returns • long: the minimal cost to rearrange the students or -1 if it’s not feasible Constraints • 1 ffi N ffi 2 × 10^5 • 1 ffi arrA[i], arrB[i] ffi 10^9 Example Input: arrA = [4, 2, 2, 2] arrB = [1, 4, 1, 2] Output: 1 Explanation: If the arrays are sorted in descending order, they would be: - arrA = [4, 2, 2, 2]
- arrB = [4, 2, 1, 1] 1 To perform a swap, replace a 2 in arrA with a 1 in arrB at a cost of min(1, 2) = 1. This single swap is sufficient, resulting in the answer of 1. Function Signature long rearrangeStudents(vector arrA, vector arrB); Implement this function to solve the problem as described above. 2 , give solution in cpp
- Assistant