S
S
Sergey2016-07-10 13:16:43
PHP
Sergey, 2016-07-10 13:16:43

How to figure out an optimization algorithm?

In general, the essence is this - there are, for example, fruit baskets, the baskets are presented in this form:

М/К-мест в корзине, К/К -количество корзин.
  М/К | К/К
  ---------
  20  |   3
  18  |   1
  16  |   2
  14  |   6
  12  |   9
  10  |   2
  8   |  10
  6   |   2
  4   |   3

In one basket there can be only apples or only pears, in short, certain fruits. For example, in one of the baskets for 20 fruits there are 18 apples, in the second - 20 pears, in the third - 5 peaches. We have six more apples and 1 pear, they need to be put in baskets, but they will not fit in the baskets that now contain apples and pears, you need to shift the fruits so that each of the baskets contains only one type of fruit. I can't figure out the algorithm. There are also fruits in small baskets, you need to take everything into account

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
aRegius, 2016-07-10
@r3l0c

Sergey, look, this option may work (I will describe the general principle, the implementation of the code, of course, is up to you
) : 10 plums, 5 bananas).
2. You fill the baskets from top to bottom (from large volumes to smaller ones). You fill as much as possible - the entire basket, leave the rest aside for the time being, do not use it.
For example:
step 1: 30 apples and 3 baskets of 20... We fill 1 basket completely, save the rest of 10 apples for later, do not use now.
step 2: 25 pears and 2 baskets of 20... We fill 1 basket completely, save the rest of 5 pears for later, do not use now.
step 3:20 oranges and 1 basket of 20... We fill 1 basket completely, there is no remainder.
step 4: 15 tangerines and 1 basket of 18... SKIP, looking for an option to fill a FULL basket...
step 5: 15 tangerines and 2 baskets of 16.. SKIP, looking for an option to fill a FULL basket...
step 6: 15 tangerines and 6 baskets of 14 each ... We fill 1 basket completely, save the rest of 1 tangerine for later, we do not use it now.
etc. .........
You sort the final balances by quantity and run it again according to the same scheme, providing, of course, for the fact that it will be necessary to partially fill the baskets, because the number of residues will be small (units).
A top-down run is needed in order to rationally use the baskets - after all, if there is 1 apple and 2 baskets (20 and 4 places, respectively), it is logical to put it in a basket for 4 places.
Something like this. Of course, I am not an expert in native algorithms, perhaps there is some kind of "interpolation method of apparent variance" for solving such problems - here I pass :). What could...

S
sitev_ru, 2016-07-10
@sitev_ru

Try it over...

O
Optimus, 2016-07-10
Pyan @marrk2

Add one more column of "free places" when you put something, you take it - you count it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question