This page is still under construction.

Parts of this page are still being built. What you see may change.

Face the Right Way

Interview

Time limit1sMemory limit128 MB

Summary
Choose a fixed block size K so that flipping blocks of K consecutive cows turns the whole row forward using the fewest operations, breaking ties by smallest K.
Level

Medium6 of 10

Topics
Greedy, Simulation, Prefix sum, Brute force
Solved
No attempts yet

Problem

Farmer John has lined up his NN (1≤N≤50001 \le N \le 5000) cows in a row. Some cows face forward, but the rest face backward, and Farmer John wants every cow to face forward.

He owns an automatic cow-flipping machine. Because he bought the discount model, the machine is permanently preset to a single value KK (1≤K≤N1 \le K \le N): every time it is used, it reverses the facing direction of some KK consecutive cows. A forward cow becomes backward and a backward cow becomes forward, while every cow stays in its original position. The machine can never act on fewer than KK cows, so it can only be applied to a block of exactly KK cows that lies entirely within the row.

Farmer John must commit to one fixed value of KK before he starts. Help him choose KK so that the machine can make all cows face forward using as few operations as possible, and let MM be that minimum number of operations for the chosen KK. If several values of KK allow the same minimum MM, choose the smallest such KK.

Input

  • Line 11: a single integer NN.
  • Lines 2…N+12 \dots N+1: line i+1i+1 contains a single character, F or B, indicating whether cow ii faces forward (F) or backward (B).

Output

Print two space-separated integers KK and MM: the chosen value of KK (the one that minimizes the number of operations, and the smallest among ties) and the corresponding minimum number of operations MM.

Hint

Consider seven cows facing backward, backward, forward, backward, forward, backward, backward.

With K=3K = 3 the machine must be used three times — on cows (1,2,3)(1,2,3), then (3,4,5)(3,4,5), then (5,6,7)(5,6,7) — after which every cow faces forward. The diagram below traces each cow's direction as the three operations are applied; > marks a cow about to be flipped.

     B > F   F   F
     B > F   F   F
     F > B > F   F
     B   B > F   F
     F   F > B > F
     B   B   B > F
     B   B   B > F

No fixed KK can finish this row in fewer than three operations, so the answer is K=3K = 3 and M=3M = 3.

Examples3

  1. Example 1

    Input
    7
    B
    B
    F
    B
    F
    B
    B
    
    Expected output
    3 3
    
  2. Example 2

    Input
    1
    F
    
    Expected output
    1 0
    
  3. Example 3

    Input
    1
    B
    
    Expected output
    1 1