I have been trying to solve this for two days now and have not come up with a good solution. Say if I have 8 character groups, like the following, how could I get them in teams of three so that all the characters are used up? For instance:
160 dwarves+
160 elves+
85 humans+
48 hobbits+
47 gnomes+
45 orcs+
31 wizards+
15 dragons= for a total of 591 characters.
Now I need to get each character into a team made up of three characters until all the characters are used up. For instance: 85 teams of (1 dwarf, 1 elf, and 1 human), 48 teams of (1 hobbit, 1 dwarf, 1 elf), etc. etc. until all the characters are used up. I need an equation that can be applied in a programming language where the number of each character class can change (for instance it might be 28 wizards instead of 31) and the sizes of all the team could be three or four or x. If I need to post on computer science section let me know.
So far the only way I have been able to figure it out is take the top most three classes, and make a team of the greatest number that is possible, and go down the list that way.
So with that method I would have:
85 teams of (1 dwarf, 1 elf, and 1 human) (which now leaves 75 dwarfs and elves unteamed, and 0 humans unteamed)
48 teams of (1 hobbit, 1 dwarf, 1 elf), (which now leaves 27 dwarves and elves unteamed, and 0 hobbits unteamed)
31 teams of (1 orc, 1 gnome, 1 wizard), (which now leaves 27 dwarfs, 27 elves, 16 gnomes, 14 orcs, 0 wizards unteamed)
16 teams of (1 gnome, 1 dwarf, 1 elf), (which now leaves 11 dwarfs, 11 elves, 0 gnomes, 14 orcs unteamed)
11 teams of (1 dragon, 1 orc, 1 dwarf), (which now leaves 0 dwarfs, 11 elves, 4 dragons, 3 orcs unteamed)
3 teams of (1 orc, 1 elf, 1 dragon).
So the final is an excess of 8 elves and 1 dragon that cannot be put into teams!!! I can code this method but and I realize there might be excess no matter how I do it but there has got to be a better way!
Thanks,
Paul
Your greedy solution of always taking the top three classes almost works; you just need to make sure that you are updating which classes really are the top three in between making each team. Here's what happens with this updated greedy algorithm (note that I'm storing them in an array of the form: $$ [Dw, El, Hu, Ho, Gn, Or, Wi, Dr] $$ but you'd probably want to use something like a priority queue):
Note that this solution was inspired from the Havel-Hakimi algorithm for showing that a degree sequence in graph theory is graphic.