pangfengliu / programmingtasks

programming tasks from my courses
67 stars 17 forks source link

Apartments and Friends #365

Open littlehug opened 5 years ago

littlehug commented 5 years ago

Task Description

We are given N people (indexed from 0 to N - 1) and M pair of friends among them, and we would like to assign each person to a line of N apartments so that the sum of the distance between friends is minimized.

We illustrate the assignment with examples. Let N be 8 and we assign them to apartment as in (0, 1, 2, 3, 4, 5, 6, 7). If person 0 and 5 are friends, then the distance between them is 5. Furthermore, if 1 and 6 are also friends, then the sum of the distance of this ordering is now 10. In this case, we can assign people as (0, 5, 1, 6, 2, 3, 4, 7), so the sum of the distance becomes 2, which is the minimum sum of the distance.

Subtasks