I have this homework, which, I think, I managed to solve, but I'm not entirely sure, because I can not prove my decision. I would like to receive comments about what I did, about its correctness and whether there is a better solution.
The problem is this: we have N groups of people, where group i has g[i] people in it. We want to put these people in two lines from S places each, so that: each group can only be placed on one line, in a continuous sequence, OR, if the group has an even number of members, we can split them into two and put them on two lines , but with the condition that they must form a rectangle (therefore, they must have the same indices on both rows). What is the minimum number of S seats required for no one to stand?
Example: Groups 4 11 . The minimum of S is 11 . We put all 4 on one line, and 11 on the second line. Other: groups 6 2 . We split 6 into two lines, as well as into two. So the minimum size is 4 .
That's what I think:
Calculate T = (sum of all groups + 1) / 2 . Store the group numbers in the array, but divide all the even x values โโinto two x / 2 values โโeach. So 4 5 becomes 2 2 5 . Now run the sum of the subset on this vector and find the minimum value greater than or equal to T that can be formed. This value is the minimum number of places in each row.
Example: 4 11 => 2 2 11 => T = (15 + 1) / 2 = 8 . We can form a minimum of 2 2 11 , which >= 8 - 11 , so the answer is.
This seems to work, at least I can't find a counter example. However, I have no evidence. It seems intuitively that you can always organize people in the required conditions with the number of places set by this algorithm.
Any hints appreciated.
Michael
source share