Dig pits to fill
Great God blog Reprint http://www.cppblog.com/MatoNo1/archive/2011/07/05/150231.aspx
The monotonous queue of multiple backpacks the junior high school will know, but never (not) write
Binary optimization I wrote it in junior high.
Never write, you'll have a guilty conscience.
Naïve equation
Dp[i,j]=max (dp[i-1,j-w[i]*k]+c[i]*k) w[i]*k<=j k<=j Div W[i]
Ignore the first dimension
Dp[j]=max (Dp[j-w[i]*k]+c[i]*k)
Complexity O (M2N)
Here is the original of the Great God:
The decision-making subscript according to the remainder of the mold W0 classification, can be divided into W0 class, respectively, corresponding to die W0 0, more than 1 ... Residual (w0-1) condition. At this point, all the decision-making j-x*w0 in the above equation are the same class. Further, set Q =j/w0 (next rounding), R=j%w0, then j=q*w0+r, for a decision subscript J ', set k= (J '-R)/w0, namely J ' =k*w0+r. It is obvious that the value range of K is: K>=0 and q-s0<=k<=q, that is, the lower bound of K is max{0, and q-s0}--is monotonous and monotonous with J.
Then, the transfer equation can be changed (here the R as a known quantity):
F[q*w0+r] = max{f[k*w0+r]+ (q-k) *v0} (K>=0 and Q-s0<=k<=q)
namely F[q*w0+r]=max{f[k*w0+r]-k*v0}+q*v0 (K>=0 and Q-s0<=k<=q)
Set G[k]=f[k*w0+r] to:
G[q]=max{g[k]-k*v0}+q*v0 (K>=0 and Q-s0<=k<=q)
This equation can already be optimized using a monotone queue!
This allows the algorithm to be drawn:
(1) from 1 to N, enumerate I, establish w[i] empty monotone queue, each element of the queue is two int value: (K, Val), representing the post-conversion subscript and decision value (G[k]-k*v[i]);
(2) from 0 to M, enumerate J, derive the value of Q, R, for queue R:
"1" Delete elements of the team's first obsolete (k<q-m[i]);
"2" f[j] the queue (here F[j] refers to the previous stage of F[j], that is f[i-1][j]. So this step must be done first), delete all the decision value Val is not greater than (F[j]-q*v[i]) element at the end of the team.
"3" takes out the first node of the team, its Val value plus q*v[i] is the value of this stage f[j].
The final f[m] is the result. The total time complexity is O (NM).
In fact, this can be generalized, that is, for the following form of the transfer equation (where H, G and W are constants, B[i] is the lower bound of the decision subscript, with I monotone):
F[i] = opt{f[i-x*h+w]}+g (b[i]<=i-x*h+w<i,x∈n)
Can be converted using the above method to perform monotonic queue optimization.
My own flagging process:
Like Dp[i]=max (Dp[j]+cost (j+1,i)) +m,cost with monotonic DP can be optimized with monotone queue
We try to transform it into a form
Dp[j]=max (Dp[j-w[i]*1]+c[i]*1,dp[j-w[i]*2]+c[i]*2]) ...
Set k<j<i,i-j,i-k can divide w[i], obviously j-k can also
dp[j]+c[i]* (i-j)/w[i]>dp[k]+c[i]* (i-k)/w[i]
dp[j]-dp[k]>c[i]* (I-K-I+J)/w[i]
dp[j]-dp[k]>c[i]* (j-k)/w[i]
Obviously not, the right side is not monotonic, j-k can not move the item
Set J=W[I]*K+R,R=J mod w[i]
Dp[w[i]*k+r]=max (dp[w[i]* (k-x) +r]+c[i]*x)
Set X>y,dp[w[i]*x+r]+c[i]*x>dp[w[i]*y+r]+c[i]*y
dp[w[i]*x+r]-dp[w[i]*y+r]>c[i]* (Y-x)
(Dp[w[i]*x+r]-dp[w[i]*y+r])/(y-x) >c[i] for a single I set up
We calculate for each J the Max X and R O (MN) For each I
Then you can do it with a monotone queue.
"POJ1276" Cash Machine