Room Test 2: sushi (+ piggy-off ring)

topic:

 

 

 

 analysis:

Because prosequence is a loop, so to break the chain to the ring , on the back of the copy sequence.

The move to an apparent while R, B will be in a, so only requires the contribution of R can be moved together.

Enumerate a cut-off point , so that the left point all R are left on the right side of all the R rely on the right. This time must meet the meaning of the questions.

But you will find that the same cut-off point, with the change in position off the ring, figured out the answer will change, so also enumerate a broken ring point .

Complexity is n ^ 2

Consider optimization:

RRBBRBBBR as a set of data, to the both sides of R discharge seen against the B to the middle, then the optimal position 5 (left-shifted two cells, do not need to shift to the right).

When the point off the ring to the right, lr will not increase, rr will not be reduced, if the cut-off point is equivalent to moving to the left, it will only bring more spend to the right nor to the left will reduce the cost (since moving It is the leftmost point).

Therefore, the ring-off point does not move leftward from the original position.

Obviously we do not need to enumerate the demarcation point from the beginning to the end, we only need to find one of the largest position, when the answer is no longer reduced, they break out (that is change the answer with the cut-off point is monotonic ).

Complexity: close O (n)

Implementation process:

Lr R is defined as the number of left, rr is the right number R, r is the desired current cost. Enumeration ring off point, enumeration demarcation point, statistical answer.

#include <bits / STDC ++ H.>
 the using  namespace STD;
 #define LL Long Long
 #define N 1000005
 #define RI Register int
 char S [N << . 1 ];
 void Work () 
{ 
    int n-= strlen (S + . 1 );
     for (I = RI . 1 ; I <= n-; I ++) S [I + n-] = S [I];
     // break-off means breakpoints ring finger does not move to the middle point of all of the B on both sides of the dividing point R on 
    int LR = 0 , RR = 0 ; 
    LL R & lt = 0 , ANS;
     for (I = RI . 1 ; I <= n-; I ++) IF(S [I] == ' R & lt ' ) = Ni +-R & lt RR, RR ++; // pretreatment in the case of the breakpoint. 1
     // the printf ( "% LLD \ n-", RR); 
    ANS = R & lt;
     int now = . 1 ;
     for (RI = POS . 1 ; POS <= n-; POS ++) { // move breakpoint 
        the while (now <+ n-POS) { // enumeration jump pointer movement boundary point 
            IF (S [now] == ' R ' ) { // mobile B would not affect the movement of R lr with statistical influence rr 
                LL tmp = R & lt; 
                rr -; // moved to the right so a R - subtracting facilitate its own statistics below Note that this cut-off point is a point in the right section inside
                now-POS + = tmp-LR- (+ N- POS . 1 -now-RR); // POS it is currently starting pos + n-1 is the end 
                 // count the number of steps: The R would have moved to the right the contribution now to the left plus minus contribution 
                LR ++; // the left ++ to facilitate the above statistics 
                IF (tmp <= r) r = tmp; // If you can not update it and then restore BREAK 
                the else {LR -; ++ RR; BREAK ;} 
            } 
            now ++ ; 
        } 
        ANS = min (ANS, R & lt);
         IF (S [POS] == ' R ' ) LR -, RR ++; // if R shift left passed right R ++ - 
        the else R & lt RR-LR + =; //If the mobile is R plus B will move all right to the left by subtracting the contribution of the right moves to the left of the contribution of R 
    } 
    the printf ( " % LLD \ n- " , ANS); 
} 
int main () 
{ 
    The freopen ( " sushi.in " , " R & lt " , stdin); 
    The freopen ( " sushi.out " , " W " , stdout);
     int T; 
    Scanf ( " % D " , & T);
     the while (T-- ) { 
        Scanf ( " % S " , S + . 1 );
        work();
    } 
} 
/ * 
1 
BBRBBRBBBRRR 
* /

 

Guess you like

Origin www.cnblogs.com/mowanying/p/11622618.html