$ Dp $ template

Digital \ (dp \)

Gugu Gu

Optimization of the queue monotone

  • Summed up a template that \ (lst \) indicates the position of the leftmost point of decision-making has not been added (to maintain does not rise, for example), \ (L (i), R (i) \) just with \ (i \) (where \ (i \) refers to the outer dimensions, not necessarily \ (i \) ) related to the left boundary and the right boundary, \ (Calc (J) \) is only with \ (j \) related to the calculation function, \ (W (I) \) only with \ (I \) related to the calculation function

  • Note that \ (f [i] \) can easily forget to determine whether the queue is empty transfer

for(int i = 1;i <= n; ++i) {
    while(lst < L(i)) ++lst
    for(;lst <= R(i); ++lst) {
        while(l <= r && calc(q[r]) < calc(lst)) --r;
        q[++r] = lst;
    }
    while(l <= r && q[l] < L(i)) ++l;
    if(l <= r) f[i] = calc(q[l])+w(i);//再次注意 此处if(l <= r)判断队是否为空极易忘!!!!!! 
}

The slope optimization

Gugu Gu

Guess you like

Origin www.cnblogs.com/mzg1805/p/11546898.html