One idea is to construct every pair and check the distance, storing the minimum. Since we need to print out the pair that constructs the minimum, we also need to store that as we loop through, being careful to print the smaller one first. A faster idea is to sort the list and loop through once, comparing adjacent elements.
We can solve this by looping over each person and their times. If they have less than 5 times, skip them. Otherwise, sort their times and average the first 3. Store the minimum average and the name of the minimum average. Since we need to split strings this is a bit ugly in C++.
The idea of this is that taking the minimum of a list is easy, but we need to take the minimum of the first element of a bunch of lists effectively here. One idea is looping over columns first and rows second, so we get the columns naturally. Another idea is to ‘transpose’ the matrix, generating a matrix such that the rows correspond to the columns of the original, and then just find the min of each list.
You might be able to figure out that we should always finish the shortest assignments first, as the amount of penalty for each hour is multiplied by the amount of unfinished assignment. To implement this, we should sort the list, and process it one by one. We can multiply the time taken by each time by (n-i) the amount of remaining tasks, and sum it over all elements.
In this problem, we need to ensure that the students are placed into each row in their correct orders. For this, we should make a list of lists representing the ideal seating plan, as well as our constructed seating plan based on the queue. We can construct a dict mapping from names to row numbers using the seating plan. Then, at the end, we can just check if the plans are the same.