Aizu - 1382 Black or White (section decision, monotonous queue optimization dp)

The meaning of problems: given a sequence of black and white colors only A, B, each length may be selected for a continuous interval of not more than k dyed to the same color, to find the minimum number of operations into a sequence A desired B .

First of all need to find the optimal solution of some of the features:

1. If the first sequence of colors A and B of the same, it can be ignored. If instead, it must be dyed the opposite color.

2. The optimal solution will not cross, because if cross appears, then the cross-section of the middle of the equivalent of doing useful work.

This can be a dp, the operation performed from left to right, i-1 before assuming positions have been dyed well, if the same i-th bit of A and B, then ignored, if instead, it then dyed Instead of color, then only need to consider the right extends to where the problem is.

Simulate what will find themselves manually, if B [l, r] x total a continuous color segment, requires a minimum of $ \ left \ lfloor \ frac {x + 2} {2} \ right \ rfloor $ operations can the a [l, r] dyed a color corresponding to (on the premise of the first operation a [l, r] all dyed B [l]), and thus can be obtained recursive formula: $ dp [i] = min \ {dp [j] + \ left \ lfloor \ frac {sum (j + 1, i) +2} {2} \ right \ rfloor \}, ik \ leqslant j <i $, dp [i] represents the dyed before position i the minimum number of operations.

How to quickly find the number of continuous color range of paragraphs in it? The number of continuous color cross sections corresponding to the number of segment boundary, and the boundary with the prefix and the number of S can be represented. For each location, if the location and position it in front of a different color, then the prefix and +1. Thus, the interval [l, r] the number of continuous color segments equal to S [r] -S [l] +1 (note not S [r] -S [l-1] +1, where the right side is equivalent to applied to the right point), then recursive becomes: $ \ begin {aligned} dp [i] = & min \ {dp [j] + \ left \ lfloor \ frac {(S [i] -S [j +1] +1) +2} {2} \ right \ rfloor \} \\ = & min \ {dp [j] + \ left \ lfloor \ frac {S [i] -S [j + 1] +3} {2} \ right \ rfloor \} \\ = & min \ {\ left \ lfloor \ frac {2dp [j] + S [i] -S [j + 1] +3} {2} \ right \ rfloor \} \\ = & \ left \ lfloor \ frac {S [i] + min \ {2dp [j] -S [j + 1] \} + 3} {2} \ right \ rfloor, ik \ leqslant j <i \ end {aligned} $

This can be recursive + monotone queue maintains a sliding window, complexity $ O (n) $

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=5e5+10,inf=0x3f3f3f3f;
 5 int n,k,S[N],dp[N],hd,tl;
 6 char a[N],b[N];
 7 struct D {int x,y;} q[N];
 8 int main() {
 9     scanf("%d%d%s%s",&n,&k,a+1,b+1);
10     for(int i=1; i<=n; ++i)S[i]=S[i-1]+(b[i]!=b[i-1]);
11     dp[0]=hd=tl=0;
12     q[tl++]= {0,-1};
13     for(int i=1; i<=n; ++i) {
14         for(; hd<tl&&i-q[hd].x>k; ++hd);
15         dp[i]=a[i]==b[i]?dp[i-1]:(S[i]+q[hd].y+3)/2;
16         D np= {i,2*dp[i]-S[i+1]};
17         for(; hd<tl&&q[tl-1].y>=np.y; --tl);
18         q[tl++]=np;
19     }
20     printf("%d\n",dp[n]);
21     return 0;
22 }

 

Guess you like

Origin www.cnblogs.com/asdfsag/p/11691180.html