Problem with the algorithm: packs of rods in a row

Well, that can be a difficult problem. This is actually an analogy for another similar problem related to my actual application, but I simplified it in this hypothetical problem for clarity. Here:

  • I have a number of rods that I need to sort. Since this is a string, only one parameter needs to be considered.
  • The rods have different lengths and different weights. There is no correlation between weight and length. A small rod can be very heavy, and a large rod can be very light.
  • Rods must be sorted by weight.
  • The real catch, however, some rods can be placed no more than a certain distance from the beginning of the line, regardless of their weight. In any case, before that, everything is fine.
  • No guarantee is given that the restrictions will be located far enough from each other to prevent the possibility of squeezing the beveled rods to overlap. In this (apparently rare) case, either the rods must be redone in some way within their limits in order to create the necessary space, or it may be necessary to find the perfect compromise solution (for example, breaking the constraint of the smallest light rod for an example).
  • In the future, you can add additional restrictions * in addition to the length restriction, to indicate specific (and not even compromising) boundaries within the line where the bars cannot intersect.

My current solution does not take into account the latest situations, and they sound as if they will include the complex work of resolving them.

, -, Javascript !

+3
4

. , . . :

  • w i: i
  • l i: i
  • m i: . , (i = 1, n, l i)

, a i, , :

J = sum (i = 1, n, w a i * sum (j = 1, i-1, l a j > > ))

:

sum (j = 1, i-1, l a j) <= m i, 1 <= <

.

, . - - . , l = {1,4} w = {1,3}. a = {1,2}, J 1 * 0 + 3 * 1 = 3, a = {2,1} ( ), J 3 * 0 + 1 * 4 = 4. , , , , .

, , . . , - , . , .

: , , . . , ( , ).


, , . , . ( - ) .

  • . , .
  • , 4
  • , . , , . , , , . ( , , 0, ), . , : , ( , ).
  • , . ( , ), . - , , , .
  • 4 , . - .
  • .

:

  • , . .

  • , , , , ( 3), "" , ( 4). - , . , , , .

  • . , O (n ^ 2), - . L, O (n), .

+1
+2

. :

  • , , .
  • , , , .
  • , , , , .
+1

Source: https://habr.com/ru/post/1767306/


All Articles