there is a common problem that you need to reorder paragraphs P and figures P of simple (P, F) order or (F, P) order.
Placement in the document: (P1, F1), (P2, F2), (P3, F3), where each tuple (P, F) can be of any order (P, F) or (F, P) there are some Fs, length which is 0, which means that F.
The challenge is to find the order for each pair (P, F).
One solution to finding the smallest number of Paiges is to apply this rule
lines_total = MIN(lines(P,F),lines(F,P)) + remaining() //this is custom addition
Well, there is no prototype of this function, but for C it goes like
calc_spend_lines(pfpairs * pairs)
Where pfpaires
typedef struct { int P; int F; } pfpaires;
And you know that you have reached the end when P = 0. For example,
All you have to do is make a function that implements this special + sign, meaning page breaks and dead lines.
This gives an O (N) solution for the minimum number of pages, but not the number of rows, since your final condition will be 0.
If you want to minimize the number of lines, you can use bisection, where you set the termination condition to something else instead of 0, and this gives you
O (N * log (L)) solution
EDIT
Since there may be other P between the current P and F, you just need to check instead of ((F, P), (P, F)) also check the blank page (N) so that the combo ((P, F) (P, N, P ), (P, P), (F, N, P)). The conclusion is that you are completing a complex algorithm, but with the same complexity. The point is that after you check one of the 4 orders, there is only one simple way to make optimal positioning, just the current state (lines) is a little more complicated.