note
LanX
<C>
my @order = sort {
$Costs[$a]+$Trims[$a]*$Costs[$b] <=> $Costs[$b]+$Trims[$b]*$Costs[$a]
} 0..$#Costs;
</C><P>
I'm pretty sure that this is the optimal solution, but I'm too lazy to write down the complete mathematical proof.<P>
The basic idea is that the terms represent the general cost difference of swapping the first and the second filter¹!<P>
So by sorting you get a solution which can't be improved by a pairwise swapping and any permutation can be represented by a sequence of pairwise swaps.<P>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-708738">
<p>Cheers Rolf
</div></div><P>
¹) see restructured formula for c in [id://1011682] and you will notice that these are the first elements and the rest is fix.<P>
<b>UPDATE:</b><P>
basic transformations lead to a simpler solution:
<c>
my @order = sort {
$Costs[$a]/(1-$Trims[$a]) <=> $Costs[$b]/(1-$Trims[$b])
} 0..$#Costs;
</c><P>
which can be precalculated to save calculation time<P>
<c>
my @order = sort {
$weight[$a] <=> $weight[$b]
} 0..$#Costs;
</c><P>
(the edge case of a division by zero must be handled as practically infinity)
1011650
1011720