Understanding of the bipartite stack optimization algorithm DP

Introduced

Binary decision stack is mainly used to meet the optimization of the DP monotonic transfer type.
I.e. we set \ (P [i] \) of \ (I \) decision point position, then the \ (P [i] \) satisfies DP monotonically increasing nature.

Since in this DP, the monotonous increase to meet the decision point, to a point, then, as the decision point to point it must be a contiguous segment.

So we can enumerate at which point a decision point, to find it as a decision point range which it corresponds.
Consider how to find a point of the interval:

Can be found, in the present case (in the case of enumeration to a point at a decision point), the point corresponding to a certain interval [L, N]. (L may be equal to N + 1)

Then we can use a stack to store the interval [L, N] is L, each new enumerator to a decision point \ (I \) , L is determined to use the stack, L is the original look better decision point, or with a new decision point \ (i \) better.
Because the decision to meet monotonic, so if a new decision points better then the L does not make sense, it can be directly ejected from the top of the stack.
We have been performing the above operations, the original decision point until it encounters a new decision point than L \ (i \) better, then it makes sense to explain this L, it is not playing.
Then we need to go half a point out as the new L, makes the point that the right point to \ (i \) is a better decision point, the left point to \ (i \) is more bad decision point.
These are the basic ideas dichotomy stack.

For example:
decision point: 1111111111 Stack: 1 (1)
decision point: 111 2222222 Stack: 1 (1) 4 (2)
decision point: 111 22222 33 is stack: 1 (1) 4 (2) 9 (3)
Decision point: 111 222 4444 stack: 1 (1) 4 (2) 7 (4)
Note: there should be two information stacks, is a L, is a transition point
(transition point is not always maintained at each point, as will increase the time complexity)

Code implementation ideas:
① the definition of a team's first pointer to the current enumeration to the decision point \ (i \) , if \ (i \) is not the first team interval contains pointers, the pointer moves forward, until \ (i \ ) is included, then the update (I \) \ a DP value. ( \ (I \) decision point is the current head of the queue pointer corresponding transfer point)
② determine the current top of the stack to L \ (i \) is a decision point better, or to the original decision points better. In terms \ (I \) and more preferably, pop stack, and then, the cycle operation ②.
③ For the current stack, determine what, if the stack is empty:

  • If it is empty, new information directly to the stack.
  • If empty, L location of the new binary decision point (the point where the original decision all the points are currently top of the stack of the original decision point), onto the stack.
    (Note: Remember Laid arbitration L = N + 1!)

summary

For most of the questions on the half stack, usually found directly after registering a monotonic.
Therefore, when using the binary stack, generally need to prove that the decision of the monotony of DP (generally used to play table method to prove), limit is still very large.
Note: There is a great restriction DP transfer restriction dichotomous stack, only in case of restrictions satisfied monotonicity can be used.
(Such CSP2019D2T2 division can use binary type stack approach had lost \ (O (N * log ( N)) \) can be treated all points)

#include<cstdio>
#include<algorithm>
using namespace std;
const long long ONE=1;
const int MOD=(1<<30);
const int MAXM=100005;
const int MAXN=40000005;
const long long INF=4e18;
int N,TYP,Pt[MAXN];
long long A[MAXN],Dp[MAXN];
int Stac[MAXN],ID[MAXN],L,R;
void Prepare(){
    scanf("%d%d",&N,&TYP);
    if(TYP==1){
        int X,Y,Z,M;
        int P[MAXM]={0},B[MAXN]={0};
        scanf("%d%d%d%d%d%d",&X,&Y,&Z,&B[1],&B[2],&M);
        for(int i=3;i<=N;i++)B[i]=(ONE*B[i-1]*X+ONE*B[i-2]*Y+Z)%MOD;
        for(int i=1,L,R;i<=M;i++){
            scanf("%d%d%d",&P[i],&L,&R);
            for(int j=P[i-1]+1;j<=P[i];j++)
                A[j]=B[j]%(R-L+1)+L;
        }
        return ;
    }
    for(int i=1;i<=N;i++)
        scanf("%lld",&A[i]);
}
int main(){
    Prepare();
    for(int i=1;i<=N;i++)
        A[i]=A[i-1]+A[i];
    for(int i=1;i<=N;i++){
        while(Stac[L+1]<=i&&L<R)L++;
        long long x=A[i]-A[ID[L]];
        Dp[i]=Dp[ID[L]]+x*x;Pt[i]=ID[i];
        int l=i,r=N+1;
        while(L<=R&&A[Stac[R]]-A[i]>=x)R--;
        if(L>R){Stac[++R]=i+1;ID[R]=i;continue;}
        while(l+1<r){
            int mid=(l+r)/2;
            if(x<=A[mid]-A[i])r=mid;
            else l=mid;
        }
        if(r==N+1)continue;
        Stac[++R]=r;ID[R]=i;
    }
    printf("%lld\n",Dp[N]);
}

example

It is mainly certificate monotonic, other parts are relatively edition.

T1 toy packing

(Although this is a question slope optimized board it ...)
The final core idea: given \ (P \) array with a constant \ (L \) , where \ (P \) array monotone increasing properties.
A transfer Dp formula: \ (Dp [I] = min \ {Dp [J] + (P [I] -P [J] -L) ^ 2 \}; \)
monotonicity proven as follows:
use of evidence to the contrary: Let there \ (a, B, C, D (a <B <C <D) \) , where \ (a \) of \ (D \) optimum decision point, \ (B \) of \ (C \ ) the optimal decision point. (I.e., to prove that the absence of)
then there is \ [Dp [A] + ( P [D] -P [A] -L) ^ 2 \ le Dp [B] + (P [D] -P [B ] -L) ^ 2 \]
\ [Dp [B] + (P [C] -P [B] -L) ^ 2 \ Le Dp [A] + (P [C] -P [A] -L) ^ 2 \]
can be obtained:
\ [(P [D] -P [A] -L) ^ 2 + (P [C] -P [B] -L) ^ 2 \ Le (P [D] -P [ B] -L) ^ 2 + (
P [C] -P [A] -L) ^ 2 \] Simplification obtained:
\ [2 * (P [B] -P [A]) * (P [D] -P [C]) \ le0 \
] and the condition does not match, so there is no case, proof that the decision-making Dp monotonicity.

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=50005;
int N,Len,A[MAXN],Pt[MAXN];
long long S[MAXN],Dp[MAXN];
int Stac[MAXN],ID[MAXN],L,R;
long long W(int i,int j){
    return (S[i]-S[j]-Len)*(S[i]-S[j]-Len);
}
int main(){
    scanf("%d%d",&N,&Len);Len++;
    for(int i=1;i<=N;i++)
        scanf("%d",&A[i]),S[i]=S[i-1]+A[i];
    for(int i=1;i<=N;i++)S[i]+=i;
    for(int i=1;i<=N;i++){
        while(Stac[L+1]<=i&&L<R)L++;
        Dp[i]=Dp[ID[L]]+W(i,ID[L]);
        while(L<=R&&Dp[ID[R]]+W(Stac[R],ID[R])>=Dp[i]+W(Stac[R],i))R--;
        if(R<L)Stac[++R]=i+1,ID[R]=i;
        else{
            int l=i,r=N+1;
            while(l+1<r){
                int mid=(l+r)/2;
                if(Dp[ID[R]]+W(mid,ID[R])>=Dp[i]+W(mid,i))r=mid;
                else l=mid;
            }
            if(r==N+1)continue;
            Stac[++R]=r;ID[R]=i;
        }
    }
    printf("%lld\n",Dp[N]);
    return 0;
}
/*
Dp[i]=Min{Dp[j]+W(i,j)};
*/

T2 poet small G

The final core idea: given \ (P \) array with a constant \ (L \) and a parameter \ (K \) , where \ (P \) array monotone increasing properties.
A transfer Dp formula: \ (Dp [I] = min \ {Dp [J] + | P [I] -P [J] -L | ^ K \}; \)
monotonicity T1 is demonstrated as follows :( thinking)
using Counterevidence: with \ (a, B, C, D (a <B <C <D) \) , where \ (a \) of \ (D \) optimum decision point, \ (B \ ) is \ (C \) optimal decision point. (I.e., to prove that the absence of)
then there is \ [Dp [A] + | P [D] -P [A] -L | ^ K \ le Dp [B] + | P [D] -P [B ] -L | ^ K \]
\ [Dp [B] + | P [C] -P [B] -L | ^ K \ Le Dp [A] + | P [C] -P [A] -L | ^ K \]
can be obtained:
\ [| P [D] -P [A] -L | ^ K + | P [C] -P [B] -L | ^ K \ Le | P [D] -P [B ] -L | ^ K + | P
[C] -P [A] -L | ^ K \] then ......
we set \ (X = P [B] -P [A], Y = P [ C] -P [B], Z

= P [D] -P [C]; \) then there is:\ [| X + Y + ZL
| ^ K + | YL | ^ K \ le | Y + ZL | ^ K + | X + YL | ^ K \] we wish to draw the \ (F (t) = | tL | ^ K \) of the image, like this:

then the image on the four points marked out.
Found \ ((+ X-ZL of the Y +) + (YL) = (ZL of the Y +) + (+ X-YL) \) , i.e., the four points of the abscissa is about \ (E = \ frac {X + 2 * Y + Z} {2} \ ) symmetry.
However, due to the four points of the distribution range, so you can classify discuss (because of the nature is the same as the right to the left, so there is only one side of the discussion):
①: from left from right (left two points, two points on the right)

In this case, apparently (\ F (Y) + F
(X + Y + Z) \ ge F (X + Y) + F (Y + Z)) \ so that the conditions do not match.
②: left and right three (one point left and right three points)

So in this case, we will \ (Y \) Flip to \ (Y '\) , then the time has \ (DX1 <DX2, DY1 < the DY2 \) , i.e. \ [F (Y + Z)
-F (Y) = F (Y + Z) -F (Y`) <F (X + Y + Z) -F (X + Y) \] i.e. there \ [F (Y + Z)
+ F (X + Y) <F (X + Y + Z) + F (Y) \] it is incompatible with the conditions.
③: Left zero fourth from the right (left of the zero point, the right four points)

这种情况下有\(DX1=DX2\),由函数斜率递增的性质可得\(DY1<DY2\)
故同②的情况,与条件不符。

综上,不存在给出情况,故该Dp式满足决策单调性。
(证完单调性后就和玩具装箱一样了,故这里就不给代码了 )

后记

打表法好啊。。。

Guess you like

Origin www.cnblogs.com/ftotl/p/11961278.html