I'm trying to solve this task:
The kingdome recruited n people. Recruit has two characteristics: ability
power and strength. Recruits must be devided into two equal-size squads:
warriors and wisards. Found separation with max efficiency level. Efficiency
of wizard is determined by ability-power, of warrior - by strength.
My first idea was to sort recruits by max parametr(or use max_heap) and then:
for recruit in sorted_recruits:
if wisards_counter!=n/2 or warriors_counter!=n/2:
if recruit.strength>recruit.ap:
summ+=recruit.strength
warriors_counter+=1
elif recruit.strength<recruit.ap:
summ+=recruit.ap
wisards_counter+=1
But then i've undestood that it's wrong aprroach. Maybe someone can advise a good data structure and heuristic to solve this task?
Btw, the second part of the task implies that parametr of recruit can be changed(increse), and we should reform teams to get maximum again. But first wanna solve at least first. Will be gratefull!
This problem can be solved with a local optimizer:
Start with a random assignment of recruits into two equally-sized groups and keep them sorted by their efficiency gain. The efficiency gain is the gain in efficiency that you would get if you move the recruit to the other set. For recruits in the wizard group, this is strength - ability
; for the other group, it is the opposite.
Then, iteratively swap two recruits (one from each group) until no more improvement is possible. Finding the swap candidates is pretty easy: Just take the two candidates with the highest efficiency gain. If the sum of efficiency gain becomes negative, you have found the optimal assignment.